home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2003 #3 / K-CD-3-2003.ISO / WinXP Service Pack 1 / xpsp1_sv_x86.exe / instcat.sq_ / instcat.sql
Encoding:
Text File  |  2002-04-22  |  749.0 KB  |  24,345 lines

  1. /*
  2. **    INSTCAT.SQL
  3. **    Installs catalog stored procedures on the Microsoft SQL Server.
  4. ** Copyright Microsoft, Inc. 1994 - 2000
  5. ** All Rights Reserved.
  6. */
  7.  
  8. /*
  9. INTERNAL NOTE:
  10. When this file is updated, you MUST change the last row inserted into
  11. spt_server_info to be version number of this file.    the convention is
  12. j.nn.bbb, where j is the major version number ('7' now), nn is the minor
  13. version number ('10' now), and bbb is the build number.
  14. insert into spt_server_info
  15.     values (500, 'SYS_SPROC_VERSION', '8.00.bbb')
  16. */
  17.  
  18. /****************************************************************************/
  19. /* This portion sets up the ability to perform all the functions in this    */
  20. /* script                                                                    */
  21. /****************************************************************************/
  22. go
  23. use master
  24. go
  25. dump tran master with no_log
  26. go
  27.  
  28. set quoted_identifier on
  29. go
  30.  
  31. if (exists (select * from sysobjects
  32.         where name = 'sp_configure' and type = 'P '))
  33. begin
  34.     execute sp_configure 'update',1
  35. end
  36. reconfigure with override
  37. go
  38.  
  39. exec sp_MS_upd_sysobj_category 1 /*Capture datetime for use below.*/
  40.  
  41. go
  42.  
  43. /*
  44. ** If old versions of tables exist, drop them.
  45. */
  46. if (exists (select * from sysobjects
  47.         where name = 'MSdatatype_info' and type = 'U '))
  48.     drop table MSdatatype_info
  49. go
  50. if (exists (select * from sysobjects
  51.         where name = 'MSdatatype_info_ext' and type = 'U '))
  52.     drop table MSdatatype_info_ext
  53. go
  54. if (exists (select * from sysobjects
  55.         where name = 'MStable_types' and type = 'U '))
  56.     drop table MStable_types
  57. go
  58. if (exists (select * from sysobjects
  59.         where name = 'MSserver_info' and type = 'U '))
  60.     drop table MSserver_info
  61. go
  62. if (exists (select * from sysobjects
  63.         where name = 'spt_table_types' and type = 'U '))
  64.     drop table spt_table_types
  65. go
  66. /*
  67. ** If tables or procs already exist, drop them.
  68. */
  69.  
  70. if (exists (select * from sysobjects
  71.         where name = 'spt_datatype_info' and type = 'U '))
  72.     drop table spt_datatype_info
  73. go
  74. if (exists (select * from sysobjects
  75.         where name = 'spt_datatype_info_ext' and type = 'U '))
  76.     drop table spt_datatype_info_ext
  77. go
  78. if (exists (select * from sysobjects
  79.         where name = 'sp_add_server_sortinfo' and type = 'P '))
  80.     drop proc sp_add_server_sortinfo
  81. go
  82. if (exists (select * from sysobjects
  83.         where name = 'sp_add_server_sortinfo75' and type = 'P '))
  84.     drop proc sp_add_server_sortinfo75
  85. go
  86. if (exists (select * from sysobjects
  87.         where name = 'spt_server_info' and type = 'U '))
  88.     drop table spt_server_info
  89. go
  90. if (exists (select * from sysobjects
  91.         where name = 'sp_tables' and type = 'P '))
  92.     drop proc sp_tables
  93. go
  94. if (exists (select * from sysobjects
  95.         where name = 'sp_statistics' and type = 'P '))
  96.     drop proc sp_statistics
  97. go
  98. if (exists (select * from sysobjects
  99.         where name = 'sp_columns' and type = 'P '))
  100.     drop proc sp_columns
  101. go
  102. if (exists (select * from sysobjects
  103.         where name = 'sp_fkeys' and type = 'P '))
  104.     drop proc sp_fkeys
  105. go
  106. if (exists (select * from sysobjects
  107.         where name = 'sp_pkeys' and type = 'P '))
  108.     drop proc sp_pkeys
  109. dump tran master with no_log
  110. go
  111.  
  112. go
  113. if (exists (select * from sysobjects
  114.         where name = 'sp_stored_procedures' and type = 'P '))
  115.     drop proc sp_stored_procedures
  116. go
  117. if (exists (select * from sysobjects
  118.         where name = 'sp_sproc_columns' and type = 'P '))
  119.     drop proc sp_sproc_columns
  120. go
  121. if (exists (select * from sysobjects
  122.         where name = 'sp_table_privileges' and type = 'P '))
  123.     drop proc sp_table_privileges
  124. go
  125. if (exists (select * from sysobjects
  126.         where name = 'sp_column_privileges' and type = 'P '))
  127.     drop proc sp_column_privileges
  128. go
  129. dump tran master with no_log
  130. go
  131. if (exists (select * from sysobjects
  132.         where name = 'sp_server_info' and type = 'P '))
  133.     drop proc sp_server_info
  134. go
  135. if (exists (select * from sysobjects
  136.         where name = 'sp_datatype_info' and type = 'P '))
  137.     drop proc sp_datatype_info
  138. go
  139. if (exists (select * from sysobjects
  140.         where name = 'sp_special_columns' and type = 'P '))
  141.     drop proc sp_special_columns
  142. go
  143. if (exists (select * from sysobjects
  144.         where name = 'sp_databases' and type = 'P '))
  145.     drop proc sp_databases
  146. go
  147. dump tran master with no_log
  148. go
  149. if (exists (select * from sysobjects
  150.         where name = 'sp_ddopen' and type = 'P '))
  151.     drop proc sp_ddopen
  152. go
  153. if (exists (select * from sysobjects
  154.         where name = 'sp_tableswc' and type = 'P '))
  155.     drop proc sp_tableswc
  156. go
  157.  
  158. if (exists (select * from sysobjects
  159.         where name = 'sp_tablecollations' and type = 'P'))
  160.     drop proc sp_tablecollations
  161. go
  162.  
  163. if (exists (select * from sysobjects
  164.         where name = 'sp_bcp_dbcmptlevel' and type = 'P'))
  165.     drop proc sp_bcp_dbcmptlevel
  166. go
  167.  
  168. dump tran master with no_log
  169. go
  170.  
  171.  
  172. if (exists (select * from sysobjects
  173.         where name = 'spt_provider_types' and type = 'U '))
  174.     begin
  175.     drop table spt_provider_types
  176.     dump tran master with no_log
  177.     end
  178. go
  179. if (exists (select * from sysobjects
  180.         where name = 'sp_catalogs_rowset' and type = 'P '))
  181.     begin
  182.     drop procedure sp_catalogs_rowset
  183.     dump tran master with no_log
  184.     end
  185. go
  186. if (exists (select * from sysobjects
  187.         where name = 'sp_column_privileges_rowset' and type = 'P '))
  188.     begin
  189.     drop procedure sp_column_privileges_rowset
  190.     dump tran master with no_log
  191.     end
  192. go
  193. if (exists (select * from sysobjects
  194.         where name = 'sp_columns_rowset' and type = 'P '))
  195.     begin
  196.     drop procedure sp_columns_rowset
  197.     dump tran master with no_log
  198.     end
  199. go
  200. if (exists (select * from sysobjects
  201.         where name = 'sp_check_constraints_rowset' and type = 'P '))
  202.     begin
  203.     drop procedure sp_check_constraints_rowset
  204.     dump tran master with no_log
  205.     end
  206. go
  207. if (exists (select * from sysobjects
  208.         where name = 'sp_check_constbytable_rowset' and type = 'P '))
  209.     begin
  210.     drop procedure sp_check_constbytable_rowset
  211.     dump tran master with no_log
  212.     end
  213. go
  214. if (exists (select * from sysobjects
  215.         where name = 'sp_foreign_keys_rowset' and type = 'P '))
  216.     begin
  217.     drop procedure sp_foreign_keys_rowset
  218.     dump tran master with no_log
  219.     end
  220. go
  221. if (exists (select * from sysobjects
  222.         where name = 'sp_indexes_rowset' and type = 'P '))
  223.     begin
  224.     drop procedure sp_indexes_rowset
  225.     dump tran master with no_log
  226.     end
  227. go
  228. if (exists (select * from sysobjects
  229.         where name = 'sp_primary_keys_rowset' and type = 'P '))
  230.     begin
  231.     drop procedure sp_primary_keys_rowset
  232.     dump tran master with no_log
  233.     end
  234. go
  235. if (exists (select * from sysobjects
  236.         where name = 'sp_procedure_params_rowset' and type = 'P '))
  237.     begin
  238.     drop procedure sp_procedure_params_rowset
  239.     dump tran master with no_log
  240.     end
  241. go
  242. if (exists (select * from sysobjects
  243.         where name = 'sp_procedures_rowset' and type = 'P '))
  244.     begin
  245.     drop procedure sp_procedures_rowset
  246.     dump tran master with no_log
  247.     end
  248. go
  249. if (exists (select * from sysobjects
  250.         where name = 'sp_provider_types_rowset' and type = 'P '))
  251.     begin
  252.     drop procedure sp_provider_types_rowset
  253.     dump tran master with no_log
  254.     end
  255. go
  256. if (exists (select * from sysobjects
  257.         where name = 'sp_schemata_rowset' and type = 'P '))
  258.     begin
  259.     drop procedure sp_schemata_rowset
  260.     dump tran master with no_log
  261.     end
  262. go
  263. if (exists (select * from sysobjects
  264.         where name = 'sp_statistics_rowset' and type = 'P '))
  265.     begin
  266.     drop procedure sp_statistics_rowset
  267.     dump tran master with no_log
  268.     end
  269. go
  270. if (exists (select * from sysobjects
  271.         where name = 'sp_tables_rowset' and type = 'P '))
  272.     begin
  273.     drop procedure sp_tables_rowset
  274.     dump tran master with no_log
  275.     end
  276. go
  277. if (exists (select * from sysobjects
  278.         where name = 'sp_tables_info_rowset' and type = 'P '))
  279.     begin
  280.     drop procedure sp_tables_info_rowset
  281.     dump tran master with no_log
  282.     end
  283. go
  284. if (exists (select * from sysobjects
  285.         where name = 'sp_tables_info_rowset_64' and type = 'P '))
  286.     begin
  287.     drop procedure sp_tables_info_rowset_64
  288.     dump tran master with no_log
  289.     end
  290. go
  291. if (exists (select * from sysobjects
  292.         where name = 'sp_table_constraints_rowset' and type = 'P '))
  293.     begin
  294.     drop proc sp_table_constraints_rowset
  295.     dump tran master with no_log
  296.     end
  297. go
  298. if (exists (select * from sysobjects
  299.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  300.     begin
  301.     drop proc sp_table_privileges_rowset
  302.     dump tran master with no_log
  303.     end
  304. go
  305. if (exists (select * from sysobjects
  306.         where name = 'sp_linkedservers_rowset' and type = 'P '))
  307.     begin
  308.     drop proc sp_linkedservers_rowset
  309.     dump tran master with no_log
  310.     end
  311. go
  312.  
  313. if (exists (select * from sysobjects
  314.         where name = 'sp_table_statistics_rowset' and type = 'P '))
  315.     begin
  316.     drop proc sp_table_statistics_rowset
  317.     dump tran master with no_log
  318.     end
  319. go
  320.  
  321. if (exists (select * from sysobjects
  322.         where name = 'sp_oledb_column_constraints' and type = 'P '))
  323.     begin
  324.     drop proc sp_oledb_column_constraints
  325.     dump tran master with no_log
  326.     end
  327. go
  328.  
  329. if (exists (select * from sysobjects
  330.         where name = 'sp_oledb_indexinfo' and type = 'P '))
  331.     begin
  332.     drop proc sp_oledb_indexinfo
  333.     dump tran master with no_log
  334.     end
  335. go
  336.  
  337. if (exists (select * from sysobjects
  338.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  339.     begin
  340.     drop proc sp_oledb_ro_usrname
  341.     dump tran master with no_log
  342.     end
  343. go
  344.  
  345. if (exists (select * from sysobjects
  346.         where name = 'sp_oledb_deflang' and type = 'P '))
  347.     begin
  348.     drop proc sp_oledb_deflang
  349.     dump tran master with no_log
  350.     end
  351. go
  352.  
  353. if (exists (select * from sysobjects
  354.         where name = 'sp_oledb_defdb' and type = 'P '))
  355.     begin
  356.     drop proc sp_oledb_defdb
  357.     dump tran master with no_log
  358.     end
  359. go
  360.  
  361. if (exists (select * from sysobjects
  362.         where name = 'sp_oledb_database' and type = 'P '))
  363.     begin
  364.     drop proc sp_oledb_database
  365.     dump tran master with no_log
  366.     end
  367. go
  368.  
  369. if (exists (select * from sysobjects
  370.         where name = 'sp_oledb_language' and type = 'P '))
  371.     begin
  372.     drop proc sp_oledb_language
  373.     dump tran master with no_log
  374.     end
  375. go
  376.  
  377. print 'creating table spt_datatype_info_ext'
  378. go
  379.  
  380. if (charindex('6.00', @@version) = 0 and
  381.     charindex('6.50', @@version) = 0 and
  382.     charindex('7.00', @@version) = 0 and
  383.     charindex('8.00', @@version) = 0)
  384. begin    /*    Pre 6.0 server */
  385.     print ''
  386.     print ''
  387.     print 'Warning:'
  388.     print 'you are installing the stored procedures '
  389.     print 'on a pre 6.0 SQL Server.'
  390.     print 'Ignore the following error.'
  391.     create table spt_datatype_info_ext (
  392.                 user_type        smallint    not null,
  393.                 CREATE_PARAMS    varchar(32) null,
  394.                 AUTO_INCREMENT    smallint null,
  395.                 typename        varchar(32))
  396. end
  397. go
  398. if (charindex('6.00', @@version) > 0 or
  399.     charindex('6.50', @@version) > 0 or
  400.     charindex('7.00', @@version) > 0 or
  401.     charindex('8.00', @@version) > 0)
  402. begin    /* 6.0 or later server */
  403.     create table spt_datatype_info_ext (
  404.                 user_type        smallint    not null,
  405.                 CREATE_PARAMS    varchar(32) null,
  406.                 AUTO_INCREMENT    smallint null,
  407.                 typename        sysname)    /* from systypes, to avoid xusertype hard-code */
  408. end
  409. go
  410.  
  411. grant select on spt_datatype_info_ext to public
  412. go
  413.  
  414.  
  415. insert into spt_datatype_info_ext
  416.     /* CHAR      user_type, create_params, auto_increment */
  417.     values             (1,    'length' ,0, 'char')
  418.  
  419. insert into spt_datatype_info_ext
  420.     /* VARCHAR     user_type, create_params, auto_increment */
  421.     values             (2,    'max length' ,0, 'varchar')
  422.  
  423. insert into spt_datatype_info_ext
  424.     /* BINARY     user_type, create_params, auto_increment */
  425.     values             (3,    'length' ,0, 'binary')
  426.  
  427. insert into spt_datatype_info_ext
  428.     /* VARBINARY user_type, create_params, auto_increment */
  429.     values             (4,    'max length' ,0, 'varbinary')
  430.  
  431. if    (charindex('6.00', @@version) > 0 or
  432.      charindex('6.50', @@version) > 0 or
  433.      charindex('7.00', @@version) > 0 or
  434.      charindex('8.00', @@version) > 0)
  435. begin    /*    Add 6.0 data types */
  436.     insert into spt_datatype_info_ext
  437.         /* DECIMAL user_type, create_params, auto_increment */
  438.         values             (26,    'precision,scale' ,0, 'decimal')
  439.  
  440.     insert into spt_datatype_info_ext
  441.         /* NUMERIC user_type, create_params, auto_increment */
  442.         values             (25,    'precision,scale' ,0, 'numeric')
  443.  
  444.     insert into spt_datatype_info_ext
  445.         /* DECIMAL IDENTITY user_type, create_params, auto_increment */
  446.         values             (26,    'precision' ,1, 'decimal')
  447.  
  448.     insert into spt_datatype_info_ext
  449.         /* NUMERIC IDENTITY user_type, create_params, auto_increment */
  450.         values             (25,    'precision' ,1, 'numeric')
  451.  
  452. end
  453. else    /*    Pre 6.0 server, add SYSNAME create param */
  454.     begin
  455.         insert into spt_datatype_info_ext
  456.             /* SYSNAME     user_type, create_param, auto_increments */
  457.             values             (18,    'max length' ,0, 'sysname')
  458.  
  459.     end
  460. go
  461.  
  462. if (charindex('7.00', @@version) = 0 and
  463.     charindex('8.00', @@version) = 0)
  464. begin
  465.     print ''
  466.     print ''
  467.     print 'Warning:'
  468.     print 'you are installing the stored procedures '
  469.     print 'on a pre 8.0 SQL Server.'
  470.     print 'Ignore the following errors.'
  471. end
  472. go
  473. if (charindex('7.00', @@version) > 0 or
  474.     charindex('8.00', @@version) > 0)
  475. begin    /*    Update usertypes for 8.0 server */
  476.     begin tran
  477.     insert into spt_datatype_info_ext
  478.         /* NCHAR     user_type, create_params, auto_increment */
  479.         values             (0,    'length' ,0, 'nchar')
  480.  
  481.     insert into spt_datatype_info_ext
  482.         /* NVARCHAR     user_type, create_params, auto_increment */
  483.         values             (0,    'max length' ,0, 'nvarchar')
  484.  
  485.     /* SET user_type TO SPHINX VALUES */
  486.     update spt_datatype_info_ext set user_type = xusertype
  487.         from spt_datatype_info_ext e, systypes t where t.name = e.typename
  488.  
  489.     commit tran
  490. end
  491. go
  492.  
  493. create unique clustered index datatypeinfoextclust on spt_datatype_info_ext(user_type,AUTO_INCREMENT)
  494. go
  495.  
  496. print 'creating table spt_datatype_info'
  497. go
  498. if (charindex('6.00', @@version) = 0 and
  499.     charindex('6.50', @@version) = 0 and
  500.     charindex('7.00', @@version) = 0 and
  501.     charindex('8.00', @@version) = 0)
  502. begin    /*    Pre 6.0 server */
  503.     print ''
  504.     print ''
  505.     print 'Warning:'
  506.     print 'you are installing the stored procedures '
  507.     print 'on a pre 6.0 SQL Server.'
  508.     print 'Ignore the following error.'
  509.     create table spt_datatype_info (
  510.         ss_dtype            tinyint     not null,
  511.         fixlen                int         null,        /* datatype len for variable, else null */
  512.         ODBCVer             tinyint     null,        /* version if needed, else null */
  513.         TYPE_NAME            varchar(32)    not null,
  514.         DATA_TYPE            smallint    not null,
  515.         data_precision        int         null,
  516.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  517.         RADIX                smallint    null,
  518.         length                int         null,
  519.         LITERAL_PREFIX        varchar(32) null,
  520.         LITERAL_SUFFIX        varchar(32) null,
  521.         CREATE_PARAMS        varchar(32) null,
  522.         NULLABLE            smallint    not null,
  523.         CASE_SENSITIVE        smallint    not null,
  524.         SEARCHABLE            smallint    not null,
  525.         UNSIGNED_ATTRIBUTE    smallint    null,
  526.         MONEY                smallint    not null,
  527.         AUTO_INCREMENT        smallint    null,
  528.         LOCAL_TYPE_NAME     varchar(32)    null,
  529.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  530.         SQL_DATA_TYPE        smallint    not null,
  531.         SQL_DATETIME_SUB    smallint    null)
  532. end
  533. go
  534. if (charindex('6.00', @@version) > 0 or
  535.     charindex('6.50', @@version) > 0 or
  536.     charindex('7.00', @@version) > 0 or
  537.     charindex('8.00', @@version) > 0)
  538. begin    /* 6.0 or later server */
  539.     create table spt_datatype_info (
  540.         ss_dtype            tinyint     not null,
  541.         fixlen                int         null,        /* datatype len for variable, else null */
  542.         ODBCVer             tinyint     null,        /* version if needed, else null */
  543.         TYPE_NAME            sysname     not null,
  544.         DATA_TYPE            smallint    not null,
  545.         data_precision        int         null,
  546.         numeric_scale        smallint    null,    /* min scale if 6.0 */
  547.         RADIX                smallint    null,
  548.         length                int         null,
  549.         LITERAL_PREFIX        varchar(32) null,
  550.         LITERAL_SUFFIX        varchar(32) null,
  551.         CREATE_PARAMS        varchar(32) null,
  552.         NULLABLE            smallint    not null,
  553.         CASE_SENSITIVE        smallint    not null,
  554.         SEARCHABLE            smallint    not null,
  555.         UNSIGNED_ATTRIBUTE    smallint    null,
  556.         MONEY                smallint    not null,
  557.         AUTO_INCREMENT        smallint    null,
  558.         LOCAL_TYPE_NAME     sysname     null,
  559.         charbin             tinyint     null, /* 0 for char/binary types, NULL for all others */
  560.         SQL_DATA_TYPE        smallint    not null,
  561.         SQL_DATETIME_SUB    smallint    null)
  562. end
  563. go
  564.  
  565. grant select on spt_datatype_info to public
  566. go
  567.  
  568. /* Get case sensitivity */
  569. if 'A' = 'A' /* create dummy begin block */
  570. begin
  571.     declare @case smallint
  572.  
  573.     begin tran
  574.     select @case = 0
  575.     select @case = 1 where 'a' <> 'A'
  576.  
  577.     /* Local Binary */
  578.     insert into spt_datatype_info values
  579.     (45,null,null,'binary',-2,null,null,null,null,'0x',null,'length',1,0,2,null,0,null,'binary',0,-2,null)
  580.  
  581.     /* Local Bit */
  582.     insert into spt_datatype_info values
  583.     (50,null,null,'bit',-7,1,0,null,1,null,null,null,0,0,2,null,0,null,'bit',null,-7,null)
  584.  
  585.     /* Local Char */
  586.     insert into spt_datatype_info values
  587.     (47,null,null,'char',1,null,null,null,null,'''','''','length',1,@case,3,null,0,null,'char',0,1,null)
  588.  
  589.     /* Local Datetime */
  590.     insert into spt_datatype_info values
  591.     (61,8,2,'datetime',11,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  592.     insert into spt_datatype_info values
  593.     (61,8,3,'datetime',93,23,3,NULL,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  594.  
  595.     /* Local Smalldatetime */
  596.     insert into spt_datatype_info values
  597.     (58,4,2,'smalldatetime',11,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  598.     insert into spt_datatype_info values
  599.     (58,4,3,'smalldatetime',93,16,0,NULL,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  600.  
  601.     /* Local Float */
  602.     insert into spt_datatype_info values
  603.     (62,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  604.     insert into spt_datatype_info values
  605.     (62,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  606.  
  607.     /* Local Real */
  608.     insert into spt_datatype_info values
  609.     (59,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  610.     insert into spt_datatype_info values
  611.     (59,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  612.  
  613.     /* Local Smallmoney */
  614.     insert into spt_datatype_info values
  615.     (122,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  616.  
  617.     /* Local Money */
  618.     insert into spt_datatype_info values
  619.     (60,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  620.  
  621.     /* Local Int */
  622.     insert into spt_datatype_info values
  623.     (56,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  624.  
  625.     commit tran
  626. end
  627. go
  628.  
  629. if 'A' = 'A' /* create dummy begin block */
  630. begin
  631.     declare @case smallint
  632.  
  633.     begin tran
  634.     select @case = 0
  635.     select @case = 1 where 'a' <> 'A'
  636.  
  637.  
  638.     /* Local Smallint */
  639.     insert into spt_datatype_info values
  640.     (52,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  641.     insert into spt_datatype_info values
  642.     (52,2,1,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  643.  
  644.     /* Local Tinyint */
  645.     insert into spt_datatype_info values
  646.     (48,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  647.  
  648.     /* Local Text */
  649.     insert into spt_datatype_info values
  650.     (35,null,null,'text',-1,2147483647,null,null,2147483647,'''','''',null,1,@case,1,null,0,null,'text',0,-1,null)
  651.  
  652.     /* Local Varbinary */
  653.     insert into spt_datatype_info values
  654.     (37,null,null,'varbinary',-3,null,null,null,null,'0x',null,'max length',1,0,2,null,0,null,'varbinary',0,-3,null)
  655.  
  656.     /* Local Varchar */
  657.     insert into spt_datatype_info values
  658.     (39,null,null,'varchar',12,null,null,null,null,'''','''','max length',1,@case,3,null,0,null,'varchar',0,12,null)
  659.  
  660.     /* Local Image */
  661.     insert into spt_datatype_info values
  662.     (34,null,null,'image',-4,2147483647,null,null,2147483647,'0x',null,null,1,0,0,null,0,null,'image',0,-4,null)
  663.  
  664.     if (charindex('6.00', @@version) > 0 or
  665.         charindex('6.50', @@version) > 0 or
  666.         charindex('7.00', @@version) > 0 or
  667.         charindex('8.00', @@version) > 0)
  668.     begin    /*    Add 6.0 data types */
  669.         /* Local Decimal */
  670.         insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  671.         (55,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  672.  
  673.         /* Local Numeric */
  674.         insert into spt_datatype_info values    /* sql server type is 'numericn' */
  675.         (63,null,null,'numeric',2    ,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  676.  
  677.         /* Identity attribute data types */
  678.  
  679.         /* Identity Int */
  680.         insert into spt_datatype_info values
  681.         (56,null,null,'int identity',4,10,0,10,null,null,null,null,0,0,2,0,0,1,'int identity',null,4,null)
  682.  
  683.         /* Identity Smallint */
  684.         insert into spt_datatype_info values
  685.         (52,null,null,'smallint identity',5,5,0,10,null,null,null,null,0,0,2,0,0,1,'smallint identity',null,5,null)
  686.  
  687.         /* Identity Tinyint */
  688.         insert into spt_datatype_info values
  689.         (48,null,null,'tinyint identity',-6,3,0,10,null,null,null,null,0,0,2,1,0,1,'tinyint identity',null,-6,null)
  690.  
  691.         /* Identity Numeric */
  692.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  693.         (55,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  694.  
  695.         /* Identity Numeric */
  696.         insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  697.         (63,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  698.  
  699.     end
  700.  
  701.     if (charindex('7.00', @@version) = 0 and
  702.         charindex('8.00', @@version) = 0)
  703.     begin    /*    Add nullable type for non-8.0 server */
  704.         /* Local Datetimn */
  705.         insert into spt_datatype_info values
  706.         (111,4,2,'smalldatetime',11,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  707.         insert into spt_datatype_info values
  708.         (111,4,3,'smalldatetime',93,16,0,10,16,'''','''',null,1,0,3,null,0,null,'smalldatetime',null,9,3)
  709.         insert into spt_datatype_info values /* sql server type is 'datetimn' */
  710.         (111,8,2,'datetime',11,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  711.         insert into spt_datatype_info values
  712.         (111,8,3,'datetime',93,23,3,10,16,'''','''',null,1,0,3,null,0,null,'datetime',null,9,3)
  713.  
  714.         /* Local Floatn */
  715.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  716.         (109,4,2,'real',7, 7,null,10,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  717.         insert into spt_datatype_info values
  718.         (109,4,3,'real',7,24,null, 2,4,null,null,null,1,0,2,0,0,0,'real',null,7,null)
  719.         insert into spt_datatype_info values /* sql server type is 'floatn' */
  720.         (109,8,2,'float',6,15,null,10,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  721.         insert into spt_datatype_info values
  722.         (109,8,3,'float',6,53,null, 2,8,null,null,null,1,0,2,0,0,0,'float',null,6,null)
  723.  
  724.         /* Local Moneyn */
  725.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  726.         (110,4,null,'smallmoney',3,10,4,10,12,'$',null,null,1,0,2,0,1,0,'smallmoney',null,3,null)
  727.         insert into spt_datatype_info values    /* sql server type is 'moneyn' */
  728.         (110,8,null,'money',3,19,4,10,21,'$',null,null,1,0,2,0,1,0,'money',null,3,null)
  729.  
  730.         /* Local Intn */
  731.         insert into spt_datatype_info values /* sql server type is 'intn' */
  732.         (38,4,null,'int',4,10,0,10,4,null,null,null,1,0,2,0,0,0,'int',null,4,null)
  733.         insert into spt_datatype_info values /* sql server type is 'intn' */
  734.         (38,2,null,'smallint',5,5,0,10,2,null,null,null,1,0,2,0,0,0,'smallint',null,5,null)
  735.         insert into spt_datatype_info values
  736.         (38,1,null,'tinyint',-6,3,0,10,1,null,null,null,1,0,2,1,0,0,'tinyint',null,-6,null)
  737.  
  738.         if (charindex('6.00', @@version) > 0 or
  739.             charindex('6.50', @@version) > 0 or
  740.             charindex('7.00', @@version) > 0 or
  741.             charindex('8.00', @@version) > 0)
  742.         begin    /*    Add 6.0 data types */
  743.             /* Local Decimaln */
  744.             insert into spt_datatype_info values    /* sql server type is 'decimaln' */
  745.             (106,null,null,'decimal',3,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'decimal',null,3,null)
  746.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  747.             (106,null,null,'decimal() identity',3,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'decimal() identity',null,3,null)
  748.  
  749.             /* Local Numericn */
  750.             insert into spt_datatype_info values    /* sql server type is 'numericn' */
  751.             (108,null,null,'numeric',2,38,0,10,null,null,null,'precision,scale',1,0,2,0,0,0,'numeric',null,2,null)
  752.             insert into spt_datatype_info values    /* sql server type is 'decmaln' */
  753.             (108,null,null,'numeric() identity',2,38,0,10,null,null,null,'precision,scale',0,0,2,0,0,1,'numeric() identity',null,2,null)
  754.         end
  755.     end
  756.  
  757.     commit tran
  758. end
  759. go
  760.  
  761. if (charindex('7.00', @@version) = 0 and
  762.     charindex('8.00', @@version) = 0)
  763. begin
  764.     print ''
  765.     print ''
  766.     print 'Warning:'
  767.     print 'you are installing the stored procedures '
  768.     print 'on a pre 8.0 SQL Server.'
  769.     print 'Ignore the following errors.'
  770. end
  771. go
  772. if (charindex('7.00', @@version) > 0 or
  773.     charindex('8.00', @@version) > 0)
  774.     begin
  775.         declare @ncase smallint
  776.  
  777.         select @ncase = 0
  778.         select @ncase = 1 where N'a' <> N'A'
  779.  
  780.         /* Local Timestamp */
  781.         insert into spt_datatype_info values
  782.         (0,null,null,'timestamp',-2,8,null,null,null,'0x',null,null,1,0,2,null,0,null,'timestamp',0,-2,null)
  783.  
  784.         /* Local GUID */
  785.         insert into spt_datatype_info values
  786.         (0,null,null,'uniqueidentifier',-11,36,null,null,null,'''','''',null,1,0,2,null,0,null,'uniqueidentifier',NULL,-11,null)
  787.  
  788.         /* Local NChar */
  789.         insert into spt_datatype_info values
  790.         (0,null,null,'nchar',-8,null,null,null,null,'N''','''','length',1,@ncase,3,null,0,null,'nchar',0,-8,null)
  791.  
  792.         /* Local NVarchar */
  793.         insert into spt_datatype_info values
  794.         (0,null,null,'nvarchar',-9,null,null,null,null,'N''','''','max length',1,@ncase,3,null,0,null,'nvarchar',0,-9,null)
  795.  
  796.         /* Local NText */
  797.         insert into spt_datatype_info values
  798.         (0,null,null,'ntext',-10,2147483646,null,null,2147483646,'N''','''',null,1,@ncase,1,null,0,null,'ntext',0,-10,null)
  799.  
  800.         if (charindex('8.00', @@version) > 0)
  801.         begin
  802. --            /* Local BIGINT */
  803.             insert into spt_datatype_info values
  804.             (127,8,null,'bigint',-5,19,0,10,null,null,null,null,1,0,2,0,0,0,'bigint',null,-5,null)
  805. --            /* Identity BIGINT */
  806.             insert into spt_datatype_info values
  807.             (127,8,null,'bigint identity',-5,19,0,10,null,null,null,null,0,0,2,0,0,1,'bigint identity',null,-5,null)
  808.  
  809.  
  810. --            /* sql_variant */
  811.             insert into spt_datatype_info values
  812.             (
  813.             98,                    --ss_dtype        
  814.             null,                --fixlen
  815.             null,                --ODBCVer
  816.             'sql_variant',        --TYPE_NAME
  817.             -150,                --SQL DATA TYPE
  818.             8000,                --data_precision
  819.             0,                    --numeric_scale
  820.             10,                    --RADIX
  821.             8000,                --length
  822.             null,                --PREFIX
  823.             null,                --SUFFIX
  824.             null,                --Create Params
  825.             1,                    --Nullable
  826.             0,                --Case sensitive
  827.             2,                    --Searchable
  828.             null,                --UNSIGNED_ATTRIBUTE
  829.             0,                --MONEY
  830.             null,                --AUTO_INCREMENT
  831.             'sql_variant',        --LOCAL TYPE NAME
  832.             0,                    --CHARBIN
  833.             -150,                --SQL_DATA_TYPE
  834.             null                --SQL_DATETIME_SUB
  835.             )
  836.         
  837.         end
  838.  
  839.  
  840.  
  841.         update spt_datatype_info set NULLABLE = 1
  842.             where TYPE_NAME = 'bit'
  843.  
  844.         update spt_datatype_info set ss_dtype = isnull((select distinct xtype from systypes
  845.             where TYPE_NAME like name+'%'),0)
  846.     end
  847. go
  848.  
  849. create unique clustered index datatypeinfoclust on spt_datatype_info(ss_dtype,fixlen,ODBCVer,AUTO_INCREMENT)
  850. go
  851.  
  852. dump tran master with no_log
  853. go
  854.  
  855. print 'creating table spt_server_info'
  856. go
  857. create table spt_server_info (
  858.               attribute_id        int NOT NULL,
  859.               attribute_name    varchar(60) NOT NULL,
  860.               attribute_value    varchar(255) NOT NULL)
  861. go
  862.  
  863. create unique clustered index serverinfoclust on spt_server_info(attribute_id)
  864. go
  865.  
  866. if (charindex('7.00', @@version) = 0 and charindex('8.00', @@version) = 0)
  867.     begin
  868.         drop procedure sp_add_server_sortinfo    /* not used by other servers */
  869.         drop procedure sp_add_server_sortinfo75    /* not used by older servers */
  870.         dump tran master with no_log
  871.     end
  872. go
  873.  
  874.  
  875. insert into spt_server_info
  876.     values (1,'DBMS_NAME','Microsoft SQL Server')
  877. insert into spt_server_info
  878.     values (2,'DBMS_VER',@@version)
  879. insert into spt_server_info
  880.     values (10,'OWNER_TERM','owner')
  881. insert into spt_server_info
  882.     values (11,'TABLE_TERM','table')
  883. insert into spt_server_info
  884.     values (12,'MAX_OWNER_NAME_LENGTH','30')
  885. insert into spt_server_info
  886.     values (13,'TABLE_LENGTH','30')
  887. insert into spt_server_info
  888.     values (14,'MAX_QUAL_LENGTH','30')
  889. insert into spt_server_info
  890.     values (15,'COLUMN_LENGTH','30')
  891. if 'A' = 'a' /* If not case sensitive server */
  892. begin
  893.     insert into spt_server_info
  894.         values (16,'IDENTIFIER_CASE','MIXED')
  895. end
  896. else
  897. begin
  898.     insert into spt_server_info
  899.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  900. end
  901. insert into spt_server_info
  902.     values (17,'TX_ISOLATION','2')
  903. if (charindex('6.00', @@version) > 0 or
  904.     charindex('6.50', @@version) > 0 or
  905.     charindex('7.00', @@version) > 0) 
  906. begin    /*    Add 6.0 collation sequence */
  907.     insert into spt_server_info
  908.         select 18,'COLLATION_SEQ',
  909.             'charset='+t2.name+' sort_order='+t1.name
  910.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  911.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  912.         from syscharsets t1, syscharsets t2, sysconfigures t3
  913.         where t1.csid=t2.id and t1.id=t3.value and t3.config=1123
  914. end
  915. else 
  916. begin    /*    Add 4.2x collation sequence */
  917.     insert into spt_server_info
  918.         select 18,'COLLATION_SEQ',
  919.             'charset='+t2.name+' sort_order='+t1.name
  920.             +' charset_num='+rtrim(convert(char(4),t1.csid))+
  921.             ' sort_order_num='+rtrim(convert(char(4),t1.id))
  922.         from syscharsets t1, syscharsets t2, sysconfigures t3
  923.         where t1.csid=t2.id and t1.id=t3.value and t3.config=123
  924. end
  925. insert into spt_server_info
  926.     values (19,'SAVEPOINT_SUPPORT','Y')
  927. insert into spt_server_info
  928.     values (20,'MULTI_RESULT_SETS','Y')
  929. insert into spt_server_info
  930.     values (22,'ACCESSIBLE_TABLES','Y')
  931. go
  932.  
  933. insert into spt_server_info
  934.     values (100,'USERID_LENGTH','30')
  935. insert into spt_server_info
  936.     values (101,'QUALIFIER_TERM','database')
  937. insert into spt_server_info
  938.     values (102,'NAMED_TRANSACTIONS','Y')
  939. insert into spt_server_info
  940.     values (103,'SPROC_AS_LANGUAGE','Y')
  941. insert into spt_server_info
  942.     values (104,'ACCESSIBLE_SPROC','Y')
  943. insert into spt_server_info
  944.     values (105,'MAX_INDEX_COLS','16')
  945. insert into spt_server_info
  946.     values (106,'RENAME_TABLE','Y')
  947. insert into spt_server_info
  948.     values (107,'RENAME_COLUMN','Y')
  949.  
  950. if (charindex('8.00', @@version) > 0)
  951. begin
  952.     /* Columns may be dropped on 8.0 or later */
  953.     insert into spt_server_info
  954.         values (108,'DROP_COLUMN','Y')
  955. end
  956. else
  957. begin
  958.     insert into spt_server_info
  959.         values (108,'DROP_COLUMN','N')
  960. end
  961.  
  962. if (charindex('8.00', @@version) > 0)
  963. begin
  964.     /* Columns size may be changed on 8.0 or later */
  965.     insert into spt_server_info
  966.         values (109,'INCREASE_COLUMN_LENGTH','Y')
  967. end
  968. else
  969. begin
  970.     insert into spt_server_info
  971.         values (109,'INCREASE_COLUMN_LENGTH','N')
  972. end
  973.  
  974. if (charindex('6.50', @@version) = 0 and
  975.     charindex('7.00', @@version) = 0 and
  976.     charindex('8.00', @@version) = 0)
  977. begin
  978.     insert into spt_server_info
  979.         values (110,'DDL_IN_TRANSACTION','N')
  980. end
  981. else
  982. begin
  983.     insert into spt_server_info
  984.         values (110,'DDL_IN_TRANSACTION','Y')
  985. end
  986.  
  987.  
  988. if (charindex('8.00', @@version) > 0)
  989. begin
  990.     /* Descending indexes allowed on 8.0 or later */
  991.     insert into spt_server_info
  992.         values (111,'DESCENDING_INDEXES','Y')
  993. end
  994. else
  995. begin
  996.     insert into spt_server_info
  997.         values (111,'DESCENDING_INDEXES','N')
  998. end
  999.  
  1000. insert into spt_server_info
  1001.     values (112,'SP_RENAME','Y')
  1002. insert into spt_server_info
  1003.     values (113,'REMOTE_SPROC','Y')
  1004. insert into spt_server_info
  1005.     values (500,'SYS_SPROC_VERSION','8.00.552')
  1006. go
  1007.  
  1008. if (charindex('7.00', @@version) > 0 or
  1009.     charindex('8.00', @@version) > 0)
  1010. begin    /*    Update values for 8.0 server */
  1011.     update spt_server_info set attribute_value = '128'
  1012.         where attribute_id in (12,13,14,15,100)
  1013. end
  1014. go
  1015.  
  1016. grant select on spt_server_info to public
  1017. go
  1018.  
  1019. print 'creating sp_column_privileges'
  1020. go
  1021.  
  1022. /*    Procedure for pre 6.50 server */
  1023. CREATE PROCEDURE sp_column_privileges (
  1024.             @table_name         varchar(32),
  1025.             @table_owner        varchar(32) = null,
  1026.             @table_qualifier    varchar(32) = null,
  1027.             @column_name        varchar(96) = null)    /* 3*32 */
  1028. as
  1029.  
  1030.     set nocount on
  1031.     declare @table_id     int
  1032.     DECLARE @full_table_name    varchar(65)    /* 2*32 + 1 */
  1033.     declare @low smallint                     /* range of userids to check */
  1034.     declare @high smallint
  1035.     declare @owner_uid smallint
  1036.  
  1037.     select @low = 0, @high = 32767
  1038.  
  1039.     if @column_name is null /*    If column name not supplied, match all */
  1040.         select @column_name = '%'
  1041.  
  1042.     if @table_qualifier is not null
  1043.     begin
  1044.         if db_name() <> @table_qualifier
  1045.         begin    /* If qualifier doesn't match current database */
  1046.             raiserror 20001 '~~Rush_5~~'
  1047.             return
  1048.         end
  1049.     end
  1050.     if @table_owner is null
  1051.     begin    /* If unqualified table name */
  1052.         SELECT @full_table_name = @table_name
  1053.     end
  1054.     else
  1055.     begin    /* Qualified table name */
  1056.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1057.     end
  1058.     /*    Get Object ID */
  1059.     select @table_id = object_id(@full_table_name)
  1060.  
  1061.     if (@@trancount <> 0)
  1062.     begin    /* If inside a transaction */
  1063.         raiserror 20003 '~~Rush_6~~'
  1064.         return
  1065.     end
  1066.  
  1067.     /*
  1068.     ** We need to create a table which will contain a row for every row to
  1069.     ** be returned to the client.
  1070.     */
  1071.  
  1072.     create table #column_priv1(
  1073.         COLUMN_NAME             varchar(32) NOT NULL,
  1074.         grantor                 smallint NOT NULL,
  1075.         grantee                 smallint NOT NULL,
  1076.         select_privilege        bit,
  1077.         select_grantable        bit,
  1078.         insert_privilege        bit,
  1079.         insert_grantable        bit,
  1080.         update_privilege        bit,
  1081.         update_grantable        bit,
  1082.         references_privilege    bit,
  1083.         references_grantable    bit,
  1084.         uid                     smallint NOT NULL,
  1085.         gid                     smallint NOT NULL)
  1086.  
  1087. /*
  1088. ** insert a row for the table owner (who has all permissions)
  1089. */
  1090.     select @owner_uid = (
  1091.         select uid
  1092.         from sysobjects
  1093.         where id = @table_id)
  1094.  
  1095.     if (charindex('6.00', @@version) > 0)
  1096.     begin
  1097.         insert into #column_priv1
  1098.             select
  1099.                 c.name,
  1100.                 u.uid,
  1101.                 @owner_uid,
  1102.                 0,
  1103.                 1,
  1104.                 0,
  1105.                 1,
  1106.                 0,
  1107.                 1,
  1108.                 0,
  1109.                 1,
  1110.                 @owner_uid,
  1111.                 0
  1112.             from syscolumns c, sysusers u
  1113.             where id = @table_id
  1114.                 and c.number = 0
  1115.                 and u.uid = 1        /* grantor is dbo of database */
  1116.     end
  1117.     else
  1118.     begin
  1119.         insert into #column_priv1
  1120.             select
  1121.                 c.name,
  1122.                 u.uid,
  1123.                 @owner_uid,
  1124.                 0,
  1125.                 1,
  1126.                 0,
  1127.                 1,
  1128.                 0,
  1129.                 1,
  1130.                 0,
  1131.                 0,
  1132.                 @owner_uid,
  1133.                 0
  1134.             from syscolumns c, sysusers u
  1135.             where id = @table_id
  1136.                 and c.number = 0
  1137.                 and u.uid = 1        /* grantor is dbo of database */
  1138.     end
  1139. /*
  1140. ** now stick in a row for every column for every user in the database
  1141. ** we will need to weed out those who have no permissions later
  1142. ** (and yes this is a cartesion product: the uid field in sysprotects
  1143. ** can also have a group id, in which case we need to extend those
  1144. ** privileges to all group members).
  1145. */
  1146.  
  1147.     insert into #column_priv1
  1148.         select distinct
  1149.             c.name,
  1150.             o.uid,
  1151.             u.uid,
  1152.             0,
  1153.             0,
  1154.             0,
  1155.             0,
  1156.             0,
  1157.             0,
  1158.             0,
  1159.             0,
  1160.             u.uid,
  1161.             u.gid
  1162.         from sysusers u, syscolumns c, sysobjects o
  1163.         where o.id = @table_id
  1164.             and c.id = o.id
  1165.             and c.number = 0
  1166.             and u.gid <> u.uid
  1167.             and u.uid <> @owner_uid
  1168.  
  1169.     /*
  1170.     ** we need to create another temporary table to contain all the various
  1171.     ** protection information for the table in question
  1172.     */
  1173.     create table #protects (
  1174.                 uid         smallint NOT NULL,
  1175.                 grantor        smallint NOT NULL,
  1176.                 action        tinyint NOT NULL,
  1177.                 protecttype tinyint NOT NULL,
  1178.                 name        varchar(32) NOT NULL)
  1179.  
  1180.     insert into #protects
  1181.         select
  1182.             p.uid,
  1183.             p.uid,
  1184.             p.action,
  1185.             p.protecttype,
  1186.             isnull(col_name(id, c.number), '~All')
  1187.             from
  1188.                 sysprotects p,
  1189.                 master.dbo.spt_values c,
  1190.                 master.dbo.spt_values a,
  1191.                 master.dbo.spt_values b
  1192.             where
  1193.                 convert(tinyint, substring(isnull(p.columns, 0x1), c.low, 1))
  1194.                     & c.high <> 0
  1195.                     and c.number <= (
  1196.                         select count(*)
  1197.                         from syscolumns
  1198.                         where id = @table_id)
  1199.                 and c.type = 'P'
  1200.                 and a.type = 'T'
  1201.                 and a.number = p.action
  1202.                 and p.action in (193,195,197,26)
  1203.                 and b.type = 'T'
  1204.                 and b.number = p.protecttype
  1205.                 and p.id = @table_id
  1206.                 and p.uid between @low and @high
  1207.  
  1208.  
  1209.     update #column_priv1
  1210.     set select_privilege = 1
  1211.         from #protects p
  1212.     where
  1213.         p.protecttype = 205
  1214.         and p.action = 193
  1215.         and (p.name = #column_priv1.COLUMN_NAME
  1216.             or name = '~All')
  1217.         and (p.uid = 0
  1218.             or p.uid = #column_priv1.gid
  1219.             or p.uid = #column_priv1.uid)
  1220.         and not exists (
  1221.             select * from #protects
  1222.             where
  1223.                 protecttype = 206
  1224.                 and action = 193
  1225.                 and (name = #column_priv1.COLUMN_NAME
  1226.                     or name = '~All')
  1227.                 and ( uid = 0
  1228.                     or uid = #column_priv1.gid
  1229.                     or uid = #column_priv1.uid))
  1230.  
  1231.     update #column_priv1
  1232.     set insert_privilege = 1
  1233.         from #protects p
  1234.     where
  1235.         p.protecttype = 205
  1236.         and p.action = 195
  1237.         and (p.name = #column_priv1.COLUMN_NAME
  1238.             or name = '~All')
  1239.         and (p.uid = 0
  1240.             or p.uid = #column_priv1.gid
  1241.             or p.uid = #column_priv1.uid)
  1242.         and not exists (
  1243.             select * from #protects
  1244.             where
  1245.                 protecttype = 206
  1246.                 and action = 195
  1247.                 and (name = #column_priv1.COLUMN_NAME
  1248.                        or name = '~All')
  1249.                 and (uid = 0
  1250.                     or uid = #column_priv1.gid
  1251.                     or uid = #column_priv1.uid))
  1252.  
  1253.     update #column_priv1
  1254.     set update_privilege = 1
  1255.         from #protects p
  1256.     where
  1257.         p.protecttype = 205
  1258.         and p.action = 197
  1259.         and (p.name = #column_priv1.COLUMN_NAME
  1260.             or name = '~All')
  1261.         and (p.uid = 0
  1262.             or p.uid = #column_priv1.gid
  1263.             or p.uid = #column_priv1.uid)
  1264.         and not exists (
  1265.             select * from #protects
  1266.                 where protecttype = 206
  1267.                 and action = 197
  1268.                 and (name = #column_priv1.COLUMN_NAME
  1269.                     or name = '~All')
  1270.                 and (uid = 0
  1271.                     or uid = #column_priv1.gid
  1272.                     or uid = #column_priv1.uid))
  1273.  
  1274.     update #column_priv1
  1275.     set references_privilege = 1
  1276.         from #protects p
  1277.     where
  1278.         p.protecttype = 205
  1279.         and p.action = 26
  1280.         and (p.name = #column_priv1.COLUMN_NAME
  1281.             or name = '~All')
  1282.         and (p.uid = 0
  1283.             or p.uid = #column_priv1.gid
  1284.             or p.uid = #column_priv1.uid)
  1285.         and not exists (
  1286.             select * from #protects
  1287.                 where protecttype = 206
  1288.                 and action = 26
  1289.                 and (name = #column_priv1.COLUMN_NAME
  1290.                     or name = '~All')
  1291.                 and (uid = 0
  1292.                     or uid = #column_priv1.gid
  1293.                     or uid = #column_priv1.uid))
  1294.  
  1295.     update #column_priv1
  1296.     set select_grantable = 1
  1297.         from #protects p
  1298.     where
  1299.         p.protecttype = 204
  1300.         and p.action = 193
  1301.         and (p.name = #column_priv1.COLUMN_NAME
  1302.             or name = '~All')
  1303.         and (p.uid = 0
  1304.             or p.uid = #column_priv1.gid
  1305.             or p.uid = #column_priv1.uid)
  1306.         and not exists (
  1307.             select * from #protects
  1308.             where
  1309.                 protecttype = 206
  1310.                 and action = 193
  1311.                 and (name = #column_priv1.COLUMN_NAME
  1312.                     or name = '~All')
  1313.                 and ( uid = 0
  1314.                     or uid = #column_priv1.gid
  1315.                     or uid = #column_priv1.uid))
  1316.  
  1317.     update #column_priv1
  1318.     set insert_grantable = 1
  1319.         from #protects p
  1320.     where
  1321.         p.protecttype = 204
  1322.         and p.action = 195
  1323.         and (p.name = #column_priv1.COLUMN_NAME
  1324.             or name = '~All')
  1325.         and (p.uid = 0
  1326.             or p.uid = #column_priv1.gid
  1327.             or p.uid = #column_priv1.uid)
  1328.         and not exists (
  1329.             select * from #protects
  1330.             where
  1331.                 protecttype = 206
  1332.                 and action = 195
  1333.                 and (name = #column_priv1.COLUMN_NAME
  1334.                     or name = '~All')
  1335.                 and ( uid = 0
  1336.                     or uid = #column_priv1.gid
  1337.                     or uid = #column_priv1.uid))
  1338.  
  1339.     update #column_priv1
  1340.     set update_grantable = 1
  1341.         from #protects p
  1342.     where
  1343.         p.protecttype = 204
  1344.         and p.action = 197
  1345.         and (p.name = #column_priv1.COLUMN_NAME
  1346.             or name = '~All')
  1347.         and (p.uid = 0
  1348.             or p.uid = #column_priv1.gid
  1349.             or p.uid = #column_priv1.uid)
  1350.         and not exists (
  1351.             select * from #protects
  1352.             where
  1353.                 protecttype = 206
  1354.                 and action = 197
  1355.                 and (name = #column_priv1.COLUMN_NAME
  1356.                     or name = '~All')
  1357.                 and ( uid = 0
  1358.                     or uid = #column_priv1.gid
  1359.                     or uid = #column_priv1.uid))
  1360.  
  1361.     update #column_priv1
  1362.     set references_grantable = 1
  1363.         from #protects p
  1364.     where
  1365.         p.protecttype = 204
  1366.         and p.action = 26
  1367.         and (p.name = #column_priv1.COLUMN_NAME
  1368.             or name = '~All')
  1369.         and (p.uid = 0
  1370.             or p.uid = #column_priv1.gid
  1371.             or p.uid = #column_priv1.uid)
  1372.         and not exists (
  1373.             select * from #protects
  1374.             where
  1375.                 protecttype = 206
  1376.                 and action = 26
  1377.                 and (name = #column_priv1.COLUMN_NAME
  1378.                     or name = '~All')
  1379.                 and ( uid = 0
  1380.                     or uid = #column_priv1.gid
  1381.                     or uid = #column_priv1.uid))
  1382.  
  1383.     create table #column_priv2(
  1384.         COLUMN_NAME     varchar(32) NOT NULL,
  1385.         grantor         smallint NULL,
  1386.         grantee         smallint NOT NULL,
  1387.         PRIVILEGE        varchar(32) NOT NULL,
  1388.         IS_GRANTABLE    varchar(3) NULL)
  1389.  
  1390.     insert into #column_priv2
  1391.         select
  1392.             COLUMN_NAME,
  1393.             grantor,
  1394.             grantee,
  1395.             'SELECT',
  1396.             'NO'
  1397.         from #column_priv1
  1398.         where select_privilege = 1 and select_grantable    = 0
  1399.  
  1400.     insert into #column_priv2
  1401.         select
  1402.             COLUMN_NAME,
  1403.             grantor,
  1404.             grantee,
  1405.             'INSERT',
  1406.             'NO'
  1407.         from #column_priv1
  1408.         where insert_privilege = 1 and insert_grantable = 0
  1409.  
  1410.     insert into #column_priv2
  1411.         select
  1412.             COLUMN_NAME,
  1413.             grantor,
  1414.             grantee,
  1415.             'UPDATE',
  1416.             'NO'
  1417.         from #column_priv1
  1418.         where update_privilege = 1 and update_grantable = 0
  1419.  
  1420.     insert into #column_priv2
  1421.         select
  1422.             COLUMN_NAME,
  1423.             grantor,
  1424.             grantee,
  1425.             'REFERENCES',
  1426.             'NO'
  1427.         from #column_priv1
  1428.         where references_privilege = 1 and references_grantable = 0
  1429.  
  1430.     insert into #column_priv2
  1431.         select
  1432.             COLUMN_NAME,
  1433.             grantor,
  1434.             grantee,
  1435.             'SELECT',
  1436.             'YES'
  1437.         from #column_priv1
  1438.         where select_grantable = 1
  1439.  
  1440.     insert into #column_priv2
  1441.         select
  1442.             COLUMN_NAME,
  1443.             grantor,
  1444.             grantee,
  1445.             'INSERT',
  1446.             'YES'
  1447.         from #column_priv1
  1448.         where insert_grantable = 1
  1449.  
  1450.     insert into #column_priv2
  1451.         select
  1452.             COLUMN_NAME,
  1453.             grantor,
  1454.             grantee,
  1455.             'UPDATE',
  1456.             'YES'
  1457.         from #column_priv1
  1458.         where update_grantable = 1
  1459.  
  1460.     insert into #column_priv2
  1461.         select
  1462.             COLUMN_NAME,
  1463.             grantor,
  1464.             grantee,
  1465.             'REFERENCES',
  1466.             'YES'
  1467.         from #column_priv1
  1468.         where references_grantable = 1
  1469.  
  1470.     select
  1471.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1472.         convert(varchar(32),user_name(@owner_uid)) TABLE_OWNER,
  1473.         @table_name TABLE_NAME,
  1474.         COLUMN_NAME,
  1475.         convert(varchar(32),user_name(grantor)) GRANTOR,
  1476.         convert(varchar(32),user_name(grantee)) GRANTEE,
  1477.         PRIVILEGE,
  1478.         IS_GRANTABLE
  1479.     from #column_priv2
  1480.     where COLUMN_NAME like @column_name
  1481.     order by 4, 7
  1482. go
  1483.  
  1484. if (charindex('6.50', @@version) = 0 and
  1485.     charindex('7.00', @@version) = 0 and
  1486.     charindex('8.00', @@version) = 0)
  1487. begin
  1488.     print ''
  1489.     print ''
  1490.     print 'Warning:'
  1491.     print 'you are installing the stored procedures '
  1492.     print 'on a pre 6.50 SQL Server.'
  1493.     print 'Ignore the following errors.'
  1494. end
  1495. else
  1496.     drop proc sp_column_privileges
  1497. go
  1498.  
  1499. /*    Procedure for 6.50 server */
  1500. CREATE PROCEDURE sp_column_privileges (
  1501.             @table_name         varchar(32),
  1502.             @table_owner        varchar(32) = null,
  1503.             @table_qualifier    varchar(32) = null,
  1504.             @column_name        varchar(96) = null)    /* 3*32 */
  1505. as
  1506.  
  1507.     declare @table_id     int
  1508.  
  1509.     if @column_name is null /*    If column name not supplied, match all */
  1510.         select @column_name = '%'
  1511.  
  1512.     if @table_qualifier is not null
  1513.     begin
  1514.         if db_name() <> @table_qualifier
  1515.         begin    /* If qualifier doesn't match current database */
  1516.             raiserror (15250, -1,-1)
  1517.             return
  1518.         end
  1519.     end
  1520.     if @table_owner is null
  1521.     begin    /* If unqualified table name */
  1522.         select @table_id = object_id(@table_name)
  1523.     end
  1524.     else
  1525.     begin    /* Qualified table name */
  1526.         select @table_id = object_id(@table_owner + '.' + @table_name)
  1527.     end
  1528.  
  1529.     select
  1530.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1531.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1532.         @table_name TABLE_NAME,
  1533.         convert(varchar(32),c.name) COLUMN_NAME,
  1534.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  1535.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  1536.         convert (varchar(32),case p.action
  1537.              when 193 then 'SELECT'
  1538.              when 195 then 'INSERT'
  1539.              when 197 then 'UPDATE'
  1540.              else 'REFERENCES'
  1541.         end) PRIVILEGE,
  1542.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1543.             else 'YES'
  1544.         end) IS_GRANTABLE
  1545.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  1546.     where
  1547.         c.id = @table_id
  1548.         and c.name like @column_name
  1549.         and c.id = p.id
  1550.         and c.id = o.id
  1551.         and case substring(p.columns, 1, 1) & 1
  1552.                 when NULL then 255    /* all columns have permission */
  1553.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1554.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1555.             end
  1556.             & v.high <> 0            /* permission applies to this column */
  1557.         and v.number <= (select count(*) from syscolumns
  1558.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1559.         and v.type = 'P'
  1560.         and v.number = c.colid
  1561.             /* expand groups */
  1562.         and ((p.uid = u.uid and u.uid <> u.gid) or
  1563.              (p.uid = u.gid and u.uid <> u.gid))
  1564.         and p.protecttype <> 206    /* only grant rows */
  1565.         and p.action in (26,193,195,197)
  1566.         and o.uid <> u.uid            /* no rows for owner */
  1567.         and not exists (            /* exclude revoke'd privileges */
  1568.             select *
  1569.             from sysprotects p1
  1570.             where
  1571.                 p1.protecttype = 206
  1572.                 and p1.action = p.action
  1573.                 and p1.id = p.id
  1574.                 and p1.uid = u.uid
  1575.                 and case substring(p1.columns, 1, 1) & 1
  1576.                         when NULL then 255    /* all columns have permission */
  1577.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1578.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1579.                     end
  1580.                     & v.high <> 0)            /* permission applies to this column */
  1581.     union
  1582.     select    /*    Add rows for table owner */
  1583.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  1584.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  1585.         @table_name TABLE_NAME,
  1586.         convert(varchar(32),col_name(@table_id, c.colid)) COLUMN_NAME,
  1587.         convert(varchar(32),user_name(u.uid)) grantor,
  1588.         convert(varchar(32),user_name(o.uid)) grantee,
  1589.         convert (varchar(32),case v.number
  1590.             when 193 then 'SELECT'
  1591.             when 195 then 'INSERT'
  1592.             when 197 then 'UPDATE'
  1593.             else 'REFERENCES'
  1594.         end) PRIVILEGE,
  1595.         convert(varchar(3),'YES') IS_GRANTABLE
  1596.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1597.     where
  1598.         c.id = @table_id
  1599.         and c.name like @column_name
  1600.         and c.id = o.id
  1601.         and u.uid = 1        /* grantor is dbo of database */
  1602.         and v.type = 'P'    /* cross product to get all exposed privileges */
  1603.         and v.number in (26,193,195,197)
  1604.         and not exists (    /* exclude revoke'd privileges */
  1605.             select *
  1606.             from sysprotects p1
  1607.             where
  1608.                 p1.protecttype = 206
  1609.                 and p1.action = v.number
  1610.                 and p1.id = o.id
  1611.                 and p1.uid = o.uid)
  1612.     order by 4, 7
  1613. go
  1614.  
  1615.  
  1616.  
  1617. if (charindex('7.00', @@version) = 0 and
  1618.     charindex('8.00', @@version) = 0)
  1619. begin
  1620.     print ''
  1621.     print ''
  1622.     print 'Warning:'
  1623.     print 'you are installing the stored procedures '
  1624.     print 'on a pre 8.0 SQL Server.'
  1625.     print 'Ignore the following errors.'
  1626. end
  1627. else
  1628.     drop proc sp_column_privileges
  1629. go
  1630.  
  1631. /*    Procedure for 8.0 server */
  1632. CREATE PROCEDURE sp_column_privileges (
  1633.             @table_name         sysname,
  1634.             @table_owner        sysname = null,
  1635.             @table_qualifier    sysname = null,
  1636.             @column_name        nvarchar(384) = null)    /* 3*128 */
  1637. as
  1638.  
  1639.     declare @table_id     int
  1640.  
  1641.     if @column_name is null /*    If column name not supplied, match all */
  1642.         select @column_name = '%'
  1643.  
  1644.     if @table_qualifier is not null
  1645.     begin
  1646.         if db_name() <> @table_qualifier
  1647.         begin    /* If qualifier doesn't match current database */
  1648.             raiserror (15250, -1,-1)
  1649.             return
  1650.         end
  1651.     end
  1652.     if @table_owner is null
  1653.     begin    /* If unqualified table name */
  1654.         select @table_id = object_id(quotename(@table_name))
  1655.     end
  1656.     else
  1657.     begin    /* Qualified table name */
  1658.         if @table_owner = N''
  1659.         begin    /* If empty owner name */
  1660.             select @table_id = 0
  1661.         end
  1662.         else
  1663.         begin
  1664.             select @table_id = object_id(quotename(@table_owner) +
  1665.                 '.' + quotename(@table_name))
  1666.         end
  1667.     end
  1668.  
  1669.     select
  1670.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1671.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1672.         @table_name TABLE_NAME,
  1673.         convert(sysname,c.name) COLUMN_NAME,
  1674.         convert(sysname,user_name(p.grantor)) GRANTOR,
  1675.         convert(sysname,user_name(u.uid)) GRANTEE,
  1676.         convert (varchar(32),case p.action
  1677.              when 193 then 'SELECT'
  1678.              when 195 then 'INSERT'
  1679.              when 197 then 'UPDATE'
  1680.              else 'REFERENCES'
  1681.         end) PRIVILEGE,
  1682.         convert (varchar(3),case when p.protecttype = 205 then 'NO'
  1683.             else 'YES'
  1684.         end) IS_GRANTABLE
  1685.     from sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  1686.     where
  1687.         c.id = @table_id
  1688.         and c.name like @column_name
  1689.         and c.id = p.id
  1690.         and c.id = o.id
  1691.         and case substring(p.columns, 1, 1) & 1
  1692.                 when NULL then 255    /* all columns have permission */
  1693.                 when 0 then convert(tinyint, substring(p.columns, v.low, 1))
  1694.                 else (~convert(tinyint, isnull(substring(p.columns, v.low, 1),0)))
  1695.             end
  1696.             & v.high <> 0            /* permission applies to this column */
  1697.         and v.number <= (select count(*) from syscolumns
  1698.             where id = @table_id)    /* ranges from 1 to # of columns in table */
  1699.         and v.type = N'P'
  1700.         and v.number = c.colid
  1701.             /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  1702.         and (u.uid > 0 and u.uid < 16384)
  1703.         and ((p.uid = u.uid) or 
  1704.              (p.uid = m.groupuid and u.uid = m.memberuid))
  1705.         and p.protecttype <> 206    /* only grant rows */
  1706.         and p.action in (26,193,195,197)
  1707.         and o.uid <> u.uid            /* no rows for owner */
  1708.         and not exists (            /* exclude revoke'd privileges */
  1709.             select *
  1710.             from sysprotects p1
  1711.             where
  1712.                 p1.protecttype = 206
  1713.                 and p1.action = p.action
  1714.                 and p1.id = p.id
  1715.                 and p1.uid = u.uid
  1716.                 and case substring(p1.columns, 1, 1) & 1
  1717.                         when NULL then 255    /* all columns have permission */
  1718.                         when 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  1719.                                             else (~convert(tinyint,isnull(substring(p.columns, v.low, 1),0)))
  1720.                     end
  1721.                     & v.high <> 0)            /* permission applies to this column */
  1722.     union
  1723.     select    /*    Add rows for table owner */
  1724.         convert(sysname,db_name()) TABLE_QUALIFIER,
  1725.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  1726.         @table_name TABLE_NAME,
  1727.         convert(sysname,col_name(@table_id, c.colid)) COLUMN_NAME,
  1728.         convert(sysname,user_name(u.uid)) grantor,
  1729.         convert(sysname,user_name(o.uid)) grantee,
  1730.         convert (varchar(32),case v.number
  1731.             when 193 then 'SELECT'
  1732.             when 195 then 'INSERT'
  1733.             when 197 then 'UPDATE'
  1734.             else 'REFERENCES'
  1735.         end) PRIVILEGE,
  1736.         convert(varchar(3),'YES') IS_GRANTABLE
  1737.     from sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  1738.     where
  1739.         c.id = @table_id
  1740.         and c.name like @column_name
  1741.         and c.id = o.id
  1742.         and u.uid = 1        /* grantor is 'dbo' of database */
  1743.         and v.type = N'P'    /* cross product to get all exposed privileges */
  1744.         and v.number in (26,193,195,197)
  1745.         and not exists (    /* exclude revoke'd privileges */
  1746.             select *
  1747.             from sysprotects p1
  1748.             where
  1749.                 p1.protecttype = 206
  1750.                 and p1.action = v.number
  1751.                 and p1.id = o.id
  1752.                 and p1.uid = o.uid)
  1753.     order by 4, 7
  1754. go
  1755.  
  1756.  
  1757. grant execute on sp_column_privileges to public
  1758. go
  1759.  
  1760. dump tran master with no_log
  1761. go
  1762.  
  1763. print 'creating sp_columns'
  1764. go
  1765.  
  1766. /*    Procedure for pre-6.0 server */
  1767. CREATE PROCEDURE sp_columns (
  1768.                  @table_name        varchar(96),
  1769.                  @table_owner        varchar(96) = null,
  1770.                  @table_qualifier    varchar(32) = null,
  1771.                  @column_name        varchar(96) = null,
  1772.                  @ODBCVer            int = 2)
  1773. AS
  1774.     DECLARE @full_table_name varchar(193)
  1775.     DECLARE @table_id        int
  1776.  
  1777.     if @ODBCVer <> 3
  1778.         select @ODBCVer = 2
  1779.     if @column_name is null /*    If column name not supplied, match all */
  1780.         select @column_name = '%'
  1781.     if @table_qualifier is not null
  1782.     begin
  1783.         if db_name() <> @table_qualifier
  1784.         begin    /* If qualifier doesn't match current database */
  1785.             raiserror 20001 '~~Rush_5~~'
  1786.             return
  1787.         end
  1788.     end
  1789.     if @table_name is null
  1790.     begin    /*    If table name not supplied, match all */
  1791.         select @table_name = '%'
  1792.     end
  1793.     if @table_owner is null
  1794.     begin    /* If unqualified table name */
  1795.         SELECT @full_table_name = @table_name
  1796.     end
  1797.     else
  1798.     begin    /* Qualified table name */
  1799.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1800.     end
  1801.  
  1802.     /*    Get Object ID */
  1803.     SELECT @table_id = object_id(@full_table_name)
  1804.     if ((charindex('%',@full_table_name) = 0) and
  1805.         (charindex('_',@full_table_name) = 0) and
  1806.         @table_id <> 0)
  1807.     begin
  1808.         /* this block is for the case where there is no pattern
  1809.              matching required for the table name */
  1810.  
  1811.         SELECT
  1812.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1813.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1814.             TABLE_NAME = convert(varchar(32),o.name),
  1815.             COLUMN_NAME = convert(varchar(32),c.name),
  1816.             d.DATA_TYPE,
  1817.             TYPE_NAME = t.name,
  1818.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1819.             LENGTH = isnull(d.length, convert(int,c.length)),
  1820.             SCALE = d.numeric_scale,
  1821.             d.RADIX,
  1822.             NULLABLE =    /* set nullability from status flag */
  1823.                 convert(smallint, convert(bit, c.status&8)),
  1824.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1825.             COLUMN_DEF = text,
  1826.             d.SQL_DATA_TYPE,
  1827.             d.SQL_DATETIME_SUB,
  1828.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1829.             ORDINAL_POSITION = convert(int,
  1830.                        (
  1831.                         select count(*)
  1832.                         from syscolumns sc
  1833.                         where sc.id     =  c.id
  1834.                           AND sc.number =  c.number
  1835.                           AND sc.colid  <= c.colid
  1836.                         )),
  1837.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1838.             SS_DATA_TYPE = c.type
  1839.         FROM
  1840.             syscolumns c,
  1841.             sysobjects o,
  1842.             syscomments m,
  1843.             master.dbo.spt_datatype_info d,
  1844.             systypes t
  1845.         WHERE
  1846.             o.id = @table_id
  1847.             AND c.id = o.id
  1848.             AND t.type = d.ss_dtype
  1849.             AND c.length = isnull(d.fixlen, c.length)
  1850.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1851.             AND o.type <> 'P'
  1852.             AND c.usertype = t.usertype
  1853.             AND c.name like @column_name
  1854.             AND c.cdefault *= m.id
  1855.             AND m.colid = 1
  1856.         ORDER BY 17
  1857.     end
  1858.     else
  1859.     begin
  1860.         /* this block is for the case where there IS pattern
  1861.              matching done on the table name */
  1862.         if @table_owner is null /*    If owner not supplied, match all */
  1863.             select @table_owner = '%'
  1864.  
  1865.         SELECT
  1866.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1867.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1868.             TABLE_NAME = convert(varchar(32),o.name),
  1869.             COLUMN_NAME = convert(varchar(32),c.name),
  1870.             d.DATA_TYPE,
  1871.             TYPE_NAME = t.name,
  1872.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  1873.             LENGTH = isnull(d.length, convert(int,c.length)),
  1874.             SCALE = d.numeric_scale,
  1875.             d.RADIX,
  1876.             NULLABLE =    /* set nullability from status flag */
  1877.                 convert(smallint, convert(bit, c.status&8)),
  1878.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  1879.             COLUMN_DEF = text,
  1880.             d.SQL_DATA_TYPE,
  1881.             d.SQL_DATETIME_SUB,
  1882.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  1883.             ORDINAL_POSITION = convert(int,
  1884.                        (
  1885.                         select count(*)
  1886.                         from syscolumns sc
  1887.                         where sc.id     =  c.id
  1888.                           AND sc.number =  c.number
  1889.                           AND sc.colid  <= c.colid
  1890.                         )),
  1891.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  1892.             SS_DATA_TYPE = c.type
  1893.         FROM
  1894.             syscolumns c,
  1895.             sysobjects o,
  1896.             syscomments m,
  1897.             master.dbo.spt_datatype_info d,
  1898.             systypes t
  1899.         WHERE
  1900.             o.name like @table_name
  1901.             AND user_name(o.uid) like @table_owner
  1902.             AND o.id = c.id
  1903.             AND t.type = d.ss_dtype
  1904.             AND c.length = isnull(d.fixlen, c.length)
  1905.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  1906.             AND o.type <> 'P'
  1907.             AND c.usertype = t.usertype
  1908.             AND c.name like @column_name
  1909.             AND c.cdefault *= m.id
  1910.             AND m.colid = 1
  1911.         ORDER BY 2, 3, 17
  1912.     end
  1913. go
  1914.  
  1915. if (charindex('6.00', @@version) = 0 and
  1916.     charindex('6.50', @@version) = 0 and
  1917.     charindex('7.00', @@version) = 0 and
  1918.     charindex('8.00', @@version) = 0)
  1919. begin
  1920.     print ''
  1921.     print ''
  1922.     print 'Warning:'
  1923.     print 'you are installing the stored procedures '
  1924.     print 'on a pre 6.0 SQL Server.'
  1925.     print 'Ignore the following error.'
  1926. end
  1927. else
  1928.     drop proc sp_columns
  1929. go
  1930.  
  1931. /*    Procedure for 6.0 and 6.50 server */
  1932. CREATE PROCEDURE sp_columns (
  1933.                  @table_name        varchar(96),
  1934.                  @table_owner        varchar(96) = null,
  1935.                  @table_qualifier    varchar(32) = null,
  1936.                  @column_name        varchar(96) = null,
  1937.                  @ODBCVer            int = 2)
  1938. AS
  1939.     DECLARE @full_table_name    varchar(193)
  1940.     DECLARE @table_id int
  1941.  
  1942.     if @ODBCVer <> 3
  1943.         select @ODBCVer = 2
  1944.     if @column_name is null /*    If column name not supplied, match all */
  1945.         select @column_name = '%'
  1946.     if @table_qualifier is not null
  1947.     begin
  1948.         if db_name() <> @table_qualifier
  1949.         begin    /* If qualifier doesn't match current database */
  1950.             raiserror (15250, -1,-1)
  1951.             return
  1952.         end
  1953.     end
  1954.     if @table_name is null
  1955.     begin
  1956.         /* If table name not supplied, match all */
  1957.         select @table_name = '%'
  1958.     end
  1959.     if @table_owner is null
  1960.     begin
  1961.         /* If unqualified table name */
  1962.         SELECT @full_table_name = @table_name
  1963.     end
  1964.     else
  1965.     begin
  1966.         /* Qualified table name */
  1967.         SELECT @full_table_name = @table_owner + '.' + @table_name
  1968.     end
  1969.  
  1970.     /* Get Object ID */
  1971.     SELECT @table_id = object_id(@full_table_name)
  1972.     if ((charindex('%',@full_table_name) = 0) and
  1973.         (charindex('[',@full_table_name) = 0) and
  1974.         (charindex('_',@full_table_name) = 0) and
  1975.         @table_id <> 0)
  1976.     begin
  1977.         /* this block is for the case where there is no pattern
  1978.             matching required for the table name */
  1979.  
  1980.         SELECT
  1981.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  1982.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  1983.             TABLE_NAME = convert(varchar(32),o.name),
  1984.             COLUMN_NAME = convert(varchar(32),c.name),
  1985.             DATA_TYPE = d.DATA_TYPE,
  1986.             TYPE_NAME = convert(varchar(32),
  1987.                     case
  1988.                         when t.usertype > 100 or t.usertype in (18,80) then t.name
  1989.                         else d.TYPE_NAME
  1990.                     end),
  1991.             "PRECISION" = convert(int,
  1992.                     case
  1993.                         when d.DATA_TYPE in (6,7) then d.data_precision    /* FLOAT/REAL */
  1994.                         else isnull(convert(int,c.prec), 2147483647)
  1995.                     end),
  1996.             LENGTH = convert(int,
  1997.                     case
  1998.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  1999.                         c.prec+2
  2000.                         else
  2001.                             isnull(d.length, c.length)
  2002.                     end),
  2003.             SCALE = convert(smallint, c.scale),
  2004.             RADIX = d.RADIX,
  2005.             NULLABLE =    /* set nullability from status flag */
  2006.                 convert(smallint, convert(bit, c.status&8)),
  2007.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2008.             COLUMN_DEF = text,
  2009.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2010.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2011.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  2012.             ORDINAL_POSITION = convert(int,
  2013.                        (
  2014.                         select count(*)
  2015.                         from syscolumns sc
  2016.                         where sc.id     =  c.id
  2017.                           AND sc.number =  c.number
  2018.                           AND sc.colid  <= c.colid
  2019.                         )),
  2020.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2021.             SS_DATA_TYPE = c.type
  2022.         FROM
  2023.             syscolumns c,
  2024.             sysobjects o,
  2025.             syscomments m,
  2026.             master.dbo.spt_datatype_info d,
  2027.             systypes t
  2028.         WHERE
  2029.             o.id = @table_id
  2030.             AND c.id = o.id
  2031.             AND t.type = d.ss_dtype
  2032.             AND c.length = isnull(d.fixlen, c.length)
  2033.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2034.             AND o.type <> 'P'
  2035.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2036.             AND c.usertype = t.usertype
  2037.             AND c.name like @column_name
  2038.             AND c.cdefault *= m.id
  2039.             AND m.colid = 1
  2040.         ORDER BY 17
  2041.     end
  2042.     else
  2043.     begin
  2044.         /* this block is for the case where there IS pattern
  2045.             matching done on the table name */
  2046.  
  2047.         if @table_owner is null /*    If owner not supplied, match all */
  2048.             select @table_owner = '%'
  2049.  
  2050.         SELECT
  2051.             TABLE_QUALIFIER = convert(varchar(32),DB_NAME()),
  2052.             TABLE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  2053.             TABLE_NAME = convert(varchar(32),o.name),
  2054.             COLUMN_NAME = convert(varchar(32),c.name),
  2055.             DATA_TYPE = d.DATA_TYPE,
  2056.             TYPE_NAME = convert(varchar(32), 
  2057.                 case 
  2058.                     when t.usertype > 100 or t.usertype in (18,80) then t.name
  2059.                     else d.TYPE_NAME
  2060.                 end),
  2061.             "PRECISION" = convert(int,
  2062.                     case
  2063.                         when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2064.                         else isnull(convert(int,c.prec), 2147483647)
  2065.                     end),
  2066.             LENGTH = convert(int,
  2067.                     case
  2068.                         when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  2069.                             c.prec+2
  2070.                         else
  2071.                             isnull(d.length, c.length)
  2072.                     end),
  2073.             SCALE = convert(smallint, c.scale),
  2074.             RADIX = d.RADIX,
  2075.             NULLABLE =    /* set nullability from status flag */
  2076.                 convert(smallint, convert(bit, c.status&8)),
  2077.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2078.             COLUMN_DEF = text,
  2079.             SQL_DATA_TYPE = d.SQL_DATA_TYPE,
  2080.             SQL_DATETIME_SUB = d.SQL_DATETIME_SUB,
  2081.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647) + d.charbin,
  2082.             ORDINAL_POSITION = convert(int,
  2083.                        (
  2084.                         select count(*)
  2085.                         from syscolumns sc
  2086.                         where sc.id     =  c.id
  2087.                           AND sc.number =  c.number
  2088.                           AND sc.colid  <= c.colid
  2089.                         )),
  2090.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO      YES',(c.status&8)+1,3))),
  2091.             SS_DATA_TYPE = c.type
  2092.         FROM
  2093.             syscolumns c,
  2094.             sysobjects o,
  2095.             syscomments m,
  2096.             master.dbo.spt_datatype_info d,
  2097.             systypes t
  2098.         WHERE
  2099.             o.name like @table_name
  2100.             AND user_name(o.uid) like @table_owner
  2101.             AND o.id = c.id
  2102.             AND t.type = d.ss_dtype
  2103.             AND c.length = isnull(d.fixlen, c.length)
  2104.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2105.             AND o.type <> 'P'
  2106.             AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  2107.             AND c.usertype = t.usertype
  2108.             AND c.name like @column_name
  2109.             AND c.cdefault *= m.id
  2110.             AND m.colid = 1
  2111.         ORDER BY 2, 3, 17
  2112.     end
  2113. go
  2114.  
  2115. if (charindex('7.00', @@version) = 0 and
  2116.     charindex('8.00', @@version) = 0)
  2117. begin
  2118.     print ''
  2119.     print ''
  2120.     print 'Warning:'
  2121.     print 'you are installing the stored procedures '
  2122.     print 'on a pre 7.0 SQL Server.'
  2123.     print 'Ignore the following errors.'
  2124. end
  2125. else
  2126.     drop proc sp_columns
  2127. go
  2128.  
  2129. /*    Procedure for 7.0 server */
  2130. CREATE PROCEDURE sp_columns (
  2131.                  @table_name        nvarchar(384),
  2132.                  @table_owner        nvarchar(384) = null,
  2133.                  @table_qualifier    sysname = null,
  2134.                  @column_name        nvarchar(384) = null,
  2135.                  @ODBCVer            int = 2)
  2136. AS
  2137.     DECLARE @full_table_name    nvarchar(769)
  2138.     DECLARE @table_id int
  2139.  
  2140.     if @ODBCVer <> 3
  2141.         select @ODBCVer = 2
  2142.     if @column_name is null /*    If column name not supplied, match all */
  2143.         select @column_name = '%'
  2144.     if @table_qualifier is not null
  2145.     begin
  2146.         if db_name() <> @table_qualifier
  2147.         begin    /* If qualifier doesn't match current database */
  2148.             raiserror (15250, -1,-1)
  2149.             return
  2150.         end
  2151.     end
  2152.     if @table_name is null
  2153.     begin    /*    If table name not supplied, match all */
  2154.         select @table_name = '%'
  2155.     end
  2156.     if @table_owner is null
  2157.     begin    /* If unqualified table name */
  2158.         SELECT @full_table_name = quotename(@table_name)
  2159.     end
  2160.     else
  2161.     begin    /* Qualified table name */
  2162.         if @table_owner = ''
  2163.         begin    /* If empty owner name */
  2164.             SELECT @full_table_name = quotename(@table_owner)
  2165.         end
  2166.         else
  2167.         begin
  2168.             SELECT @full_table_name = quotename(@table_owner) +
  2169.                 '.' + quotename(@table_name)
  2170.         end
  2171.     end
  2172.  
  2173.     /*    Get Object ID */
  2174.     SELECT @table_id = object_id(@full_table_name)
  2175.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2176.         (isnull(charindex('[', @table_name),0) = 0) and
  2177.         (isnull(charindex('[', @table_owner),0) = 0) and
  2178.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2179.         @table_id <> 0)
  2180.     begin
  2181.         /* this block is for the case where there is no pattern
  2182.             matching required for the table name */
  2183.         
  2184.         SELECT
  2185.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2186.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2187.             TABLE_NAME = convert(sysname,o.name),
  2188.             COLUMN_NAME = convert(sysname,c.name),
  2189.             d.DATA_TYPE,
  2190.             convert (sysname,case
  2191.                 when t.xusertype > 255 then t.name
  2192.                 else d.TYPE_NAME
  2193.             end) TYPE_NAME,
  2194.             convert(int,case
  2195.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2196.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2197.             end) "PRECISION",
  2198.             convert(int,case
  2199.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2200.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2201.                 else
  2202.                     isnull(d.length, c.length)
  2203.             end) LENGTH,
  2204.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2205.             d.RADIX,
  2206.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2207.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2208.             COLUMN_DEF = text,
  2209.             d.SQL_DATA_TYPE,
  2210.             d.SQL_DATETIME_SUB,
  2211.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2212.             ORDINAL_POSITION = convert(int,
  2213.                        (
  2214.                         select count(*)
  2215.                         from syscolumns sc
  2216.                         where sc.id     =  c.id
  2217.                           AND sc.number =  c.number
  2218.                           AND sc.colid  <= c.colid
  2219.                         )),
  2220.             IS_NULLABLE = convert(varchar(254),
  2221.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2222.             SS_DATA_TYPE = c.type
  2223.         FROM
  2224.             sysobjects o,
  2225.             master.dbo.spt_datatype_info d,
  2226.             systypes t,
  2227.             syscolumns c
  2228.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2229.                 AND m.colid = 1
  2230.         WHERE
  2231.             o.id = @table_id
  2232.             AND c.id = o.id
  2233.             AND t.xtype = d.ss_dtype
  2234.             AND c.length = isnull(d.fixlen, c.length)
  2235.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2236.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2237.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2238.             AND c.xusertype = t.xusertype
  2239.             AND c.name like @column_name
  2240.         ORDER BY 17
  2241.     end
  2242.     else
  2243.     begin
  2244.         /* this block is for the case where there IS pattern
  2245.             matching done on the table name */
  2246.  
  2247.         if @table_owner is null /*    If owner not supplied, match all */
  2248.             select @table_owner = '%'
  2249.  
  2250.         SELECT
  2251.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2252.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2253.             TABLE_NAME = convert(sysname,o.name),
  2254.             COLUMN_NAME = convert(sysname,c.name),
  2255.             d.DATA_TYPE,
  2256.             convert (sysname,case
  2257.                 when t.xusertype > 255 then t.name
  2258.                 else d.TYPE_NAME
  2259.             end) TYPE_NAME,
  2260.             convert(int,case
  2261.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2262.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2263.             end) "PRECISION",
  2264.             convert(int,case
  2265.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2266.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2267.                 else
  2268.                     isnull(d.length, c.length)
  2269.             end) LENGTH,
  2270.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2271.             d.RADIX,
  2272.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2273.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2274.             COLUMN_DEF = text,
  2275.             d.SQL_DATA_TYPE,
  2276.             d.SQL_DATETIME_SUB,
  2277.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2278.             ORDINAL_POSITION = convert(int,
  2279.                        (
  2280.                         select count(*)
  2281.                         from syscolumns sc
  2282.                         where sc.id     =  c.id
  2283.                           AND sc.number =  c.number
  2284.                           AND sc.colid  <= c.colid
  2285.                         )),
  2286.             IS_NULLABLE = convert(varchar(254),
  2287.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2288.             SS_DATA_TYPE = c.type
  2289.         FROM
  2290.             sysobjects o,
  2291.             master.dbo.spt_datatype_info d,
  2292.             systypes t,
  2293.             syscolumns c
  2294.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2295.                 AND m.colid = 1
  2296.         WHERE
  2297.             o.name like @table_name
  2298.             AND user_name(o.uid) like @table_owner
  2299.             AND o.id = c.id
  2300.             AND t.xtype = d.ss_dtype
  2301.             AND c.length = isnull(d.fixlen, c.length)
  2302.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2303.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2304.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2305.             AND c.xusertype = t.xusertype
  2306.             AND c.name like @column_name
  2307.         ORDER BY 2, 3, 17
  2308.     end
  2309. go
  2310.  
  2311. if (charindex('8.00', @@version) = 0)
  2312. begin
  2313.     print ''
  2314.     print ''
  2315.     print 'Warning:'
  2316.     print 'you are installing the stored procedures '
  2317.     print 'on a pre 8.0 SQL Server.'
  2318.     print 'Ignore the following errors.'
  2319. end
  2320. else
  2321.     drop proc sp_columns
  2322. go
  2323.  
  2324. /*    Procedure for 8.0 server */
  2325. CREATE PROCEDURE sp_columns (
  2326.                  @table_name        nvarchar(384),
  2327.                  @table_owner        nvarchar(384) = null,
  2328.                  @table_qualifier    sysname = null,
  2329.                  @column_name        nvarchar(384) = null,
  2330.                  @ODBCVer            int = 2)
  2331. AS
  2332.     DECLARE @full_table_name    nvarchar(769)
  2333.     DECLARE @table_id int
  2334.  
  2335.     if @ODBCVer <> 3
  2336.         select @ODBCVer = 2
  2337.     if @column_name is null /*    If column name not supplied, match all */
  2338.         select @column_name = '%'
  2339.     if @table_qualifier is not null
  2340.     begin
  2341.         if db_name() <> @table_qualifier
  2342.         begin    /* If qualifier doesn't match current database */
  2343.             raiserror (15250, -1,-1)
  2344.             return
  2345.         end
  2346.     end
  2347.     if @table_name is null
  2348.     begin    /*    If table name not supplied, match all */
  2349.         select @table_name = '%'
  2350.     end
  2351.     if @table_owner is null
  2352.     begin    /* If unqualified table name */
  2353.         SELECT @full_table_name = quotename(@table_name)
  2354.     end
  2355.     else
  2356.     begin    /* Qualified table name */
  2357.         if @table_owner = ''
  2358.         begin    /* If empty owner name */
  2359.             SELECT @full_table_name = quotename(@table_owner)
  2360.         end
  2361.         else
  2362.         begin
  2363.             SELECT @full_table_name = quotename(@table_owner) +
  2364.                 '.' + quotename(@table_name)
  2365.         end
  2366.     end
  2367.  
  2368.     /*    Get Object ID */
  2369.     SELECT @table_id = object_id(@full_table_name)
  2370.     if ((isnull(charindex('%', @full_table_name),0) = 0) and
  2371.         (isnull(charindex('[', @table_name),0) = 0) and
  2372.         (isnull(charindex('[', @table_owner),0) = 0) and
  2373.         (isnull(charindex('_', @full_table_name),0) = 0) and
  2374.         @table_id <> 0)
  2375.     begin
  2376.         /* this block is for the case where there is no pattern
  2377.             matching required for the table name */
  2378.         
  2379.         SELECT
  2380.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2381.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2382.             TABLE_NAME = convert(sysname,o.name),
  2383.             COLUMN_NAME = convert(sysname,c.name),
  2384.             d.DATA_TYPE,
  2385.             convert (sysname,case
  2386.                 when t.xusertype > 255 then t.name
  2387.                 else d.TYPE_NAME collate database_default
  2388.             end) TYPE_NAME,
  2389.             convert(int,case
  2390.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2391.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2392.             end) "PRECISION",
  2393.             convert(int,case
  2394.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2395.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2396.                 else
  2397.                     isnull(d.length, c.length)
  2398.             end) LENGTH,
  2399.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2400.             d.RADIX,
  2401.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2402.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2403.             COLUMN_DEF = text,
  2404.             d.SQL_DATA_TYPE,
  2405.             d.SQL_DATETIME_SUB,
  2406.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2407.             ORDINAL_POSITION = convert(int,
  2408.                        (
  2409.                         select count(*)
  2410.                         from syscolumns sc
  2411.                         where sc.id     =  c.id
  2412.                           AND sc.number =  c.number
  2413.                           AND sc.colid  <= c.colid
  2414.                         )),
  2415.             IS_NULLABLE = convert(varchar(254),
  2416.                 substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3)),
  2417.             SS_DATA_TYPE = c.type
  2418.         FROM
  2419.             sysobjects o,
  2420.             master.dbo.spt_datatype_info d,
  2421.             systypes t,
  2422.             syscolumns c
  2423.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2424.                 AND m.colid = 1
  2425.         WHERE
  2426.             o.id = @table_id
  2427.             AND c.id = o.id
  2428.             AND t.xtype = d.ss_dtype
  2429.             AND c.length = isnull(d.fixlen, c.length)
  2430.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2431.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2432.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2433.             AND c.xusertype = t.xusertype
  2434.             AND c.name like @column_name
  2435.         ORDER BY 17
  2436.     end
  2437.     else
  2438.     begin
  2439.         /* this block is for the case where there IS pattern
  2440.             matching done on the table name */
  2441.  
  2442.         if @table_owner is null /*    If owner not supplied, match all */
  2443.             select @table_owner = '%'
  2444.  
  2445.         SELECT
  2446.             TABLE_QUALIFIER = convert(sysname,DB_NAME()),
  2447.             TABLE_OWNER = convert(sysname,USER_NAME(o.uid)),
  2448.             TABLE_NAME = convert(sysname,o.name),
  2449.             COLUMN_NAME = convert(sysname,c.name),
  2450.             d.DATA_TYPE,
  2451.             convert (sysname,case
  2452.                 when t.xusertype > 255 then t.name
  2453.                 else d.TYPE_NAME collate database_default
  2454.             end) TYPE_NAME,
  2455.             convert(int,case
  2456.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2457.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  2458.             end) "PRECISION",
  2459.             convert(int,case
  2460.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  2461.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  2462.                 else
  2463.                     isnull(d.length, c.length)
  2464.             end) LENGTH,
  2465.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  2466.             d.RADIX,
  2467.             NULLABLE = convert(smallint, ColumnProperty (c.id, c.name, 'AllowsNull')),
  2468.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  2469.             COLUMN_DEF = text,
  2470.             d.SQL_DATA_TYPE,
  2471.             d.SQL_DATETIME_SUB,
  2472.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  2473.             ORDINAL_POSITION = convert(int,
  2474.                        (
  2475.                         select count(*)
  2476.                         from syscolumns sc
  2477.                         where sc.id     =  c.id
  2478.                           AND sc.number =  c.number
  2479.                           AND sc.colid  <= c.colid
  2480.                         )),
  2481.             IS_NULLABLE = convert(varchar(254),
  2482.                 rtrim(substring('NO YES',(ColumnProperty (c.id, c.name, 'AllowsNull')*3)+1,3))),
  2483.             SS_DATA_TYPE = c.type
  2484.         FROM
  2485.             sysobjects o,
  2486.             master.dbo.spt_datatype_info d,
  2487.             systypes t,
  2488.             syscolumns c
  2489.             LEFT OUTER JOIN syscomments m on c.cdefault = m.id
  2490.                 AND m.colid = 1
  2491.         WHERE
  2492.             o.name like @table_name
  2493.             AND user_name(o.uid) like @table_owner
  2494.             AND o.id = c.id
  2495.             AND t.xtype = d.ss_dtype
  2496.             AND c.length = isnull(d.fixlen, c.length)
  2497.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2498.             AND (o.type not in ('P', 'FN', 'TF', 'IF') OR (o.type in ('TF', 'IF') and c.number = 0))
  2499.             AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  2500.             AND c.xusertype = t.xusertype
  2501.             AND c.name like @column_name
  2502.         ORDER BY 2, 3, 17
  2503.     end
  2504. go
  2505.  
  2506. grant execute on sp_columns to public
  2507. go
  2508.  
  2509. dump tran master with no_log
  2510. go
  2511.  
  2512. print 'creating sp_databases'
  2513. go
  2514.  
  2515. /*    Procedure for pre-7.0 server */
  2516. create proc sp_databases
  2517. as
  2518.     set nocount on
  2519.     /* Use temporary table to sum up database size w/o using group by */
  2520.     create table #databases (
  2521.                   DATABASE_NAME varchar(32) NOT NULL,
  2522.                   size int NOT NULL)
  2523.  
  2524.     /* Insert row for each database */
  2525.     insert into #databases
  2526.         select
  2527.             name,
  2528.             (select sum(size) from master.dbo.sysusages
  2529.                 where dbid = d.dbid)
  2530.         from master.dbo.sysdatabases d
  2531.  
  2532.     select
  2533.          DATABASE_NAME,
  2534.          DATABASE_SIZE = size*2,    /* Convert from 2048 byte pages to K */
  2535.          REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2536.     from #databases
  2537.     order by 1
  2538. go
  2539.  
  2540. if (charindex('7.00', @@version) = 0 and
  2541.     charindex('8.00', @@version) = 0)
  2542. begin
  2543.     print ''
  2544.     print ''
  2545.     print 'Warning:'
  2546.     print 'you are installing the stored procedures '
  2547.     print 'on a pre 8.0 SQL Server.'
  2548.     print 'Ignore the following errors.'
  2549. end
  2550. else
  2551.     drop proc sp_databases
  2552. go
  2553.  
  2554. /*    Procedure for 8.0 server */
  2555. create proc sp_databases
  2556. as
  2557.     set nocount on
  2558.     declare @name sysname
  2559.     declare @SQL  nvarchar(600)
  2560.  
  2561.     /* Use temporary table to sum up database size w/o using group by */
  2562.     create table #databases (
  2563.                   DATABASE_NAME sysname NOT NULL,
  2564.                   size int NOT NULL)
  2565.  
  2566.     declare c1 cursor for 
  2567.         select name from master.dbo.sysdatabases
  2568.             where has_dbaccess(name) = 1 -- Only look at databases to which we have access
  2569.  
  2570.     open c1
  2571.     fetch c1 into @name
  2572.  
  2573.     while @@fetch_status >= 0
  2574.     begin
  2575.         select @SQL = 'insert into #databases
  2576.                 select N'''+ @name + ''', sum(size) from '
  2577.                 + QuoteName(@name) + '.dbo.sysfiles'
  2578.         /* Insert row for each database */
  2579.         execute (@SQL)
  2580.         fetch c1 into @name
  2581.     end
  2582.     deallocate c1
  2583.  
  2584.     select    
  2585.         DATABASE_NAME,
  2586.         DATABASE_SIZE = size*8,/* Convert from 8192 byte pages to K */
  2587.         REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  2588.     from #databases
  2589.     order by 1
  2590. go
  2591.  
  2592. grant execute on sp_databases to public
  2593. go
  2594.  
  2595. dump tran master with no_log
  2596. go
  2597.  
  2598. print 'creating sp_datatype_info'
  2599. go
  2600.  
  2601. /*    Procedure for pre-6.0 server */
  2602. create proc sp_datatype_info
  2603.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2604. as
  2605.     if @ODBCVer <> 3
  2606.         select @ODBCVer = 2
  2607.     if @data_type = 0
  2608.         select
  2609.             TYPE_NAME = t.name,
  2610.             d.DATA_TYPE,
  2611.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2612.             d.LITERAL_PREFIX,
  2613.             d.LITERAL_SUFFIX,
  2614.             e.CREATE_PARAMS,
  2615.             d.NULLABLE,
  2616.             d.CASE_SENSITIVE,
  2617.             d.SEARCHABLE,
  2618.             d.UNSIGNED_ATTRIBUTE,
  2619.             d.MONEY,
  2620.             d.AUTO_INCREMENT,
  2621.             LOCAL_TYPE_NAME = t.name,
  2622.             MINIMUM_SCALE = d.numeric_scale,
  2623.             MAXIMUM_SCALE = d.numeric_scale,
  2624.             d.SQL_DATA_TYPE,
  2625.             d.SQL_DATETIME_SUB,
  2626.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2627.             INTERVAL_PRECISION = convert(smallint,NULL),
  2628.             USERTYPE = t.usertype
  2629.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2630.         where
  2631.             d.ss_dtype = t.type
  2632.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2633.             and t.usertype *= e.user_type
  2634.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2635.                 t.usertype > 100)
  2636.         order by 2, 12, 11, t.usertype
  2637.  
  2638.     else
  2639.         select
  2640.             TYPE_NAME = t.name,
  2641.             d.DATA_TYPE,
  2642.             "PRECISION" = isnull(d.data_precision, convert(int,t.length)),
  2643.             d.LITERAL_PREFIX,
  2644.             d.LITERAL_SUFFIX,
  2645.             e.CREATE_PARAMS,
  2646.             d.NULLABLE,
  2647.             d.CASE_SENSITIVE,
  2648.             d.SEARCHABLE,
  2649.             d.UNSIGNED_ATTRIBUTE,
  2650.             d.MONEY,
  2651.             d.AUTO_INCREMENT,
  2652.             LOCAL_TYPE_NAME = t.name,
  2653.             MINIMUM_SCALE = d.numeric_scale,
  2654.             MAXIMUM_SCALE = d.numeric_scale,
  2655.             d.SQL_DATA_TYPE,
  2656.             d.SQL_DATETIME_SUB,
  2657.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2658.             INTERVAL_PRECISION = convert(smallint,NULL),
  2659.             USERTYPE = t.usertype
  2660.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2661.         where
  2662.             DATA_TYPE = @data_type
  2663.             and d.ss_dtype = t.type
  2664.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2665.             and t.usertype *= e.user_type
  2666.             and (t.type not in (111,109,38,110) or /* get rid of nullable types */
  2667.                 t.usertype > 100)
  2668.         order by 12, 11, t.usertype
  2669.  
  2670. go
  2671.  
  2672. if (charindex('6.00', @@version) = 0 and
  2673.     charindex('6.50', @@version) = 0 and
  2674.     charindex('7.00', @@version) = 0 and
  2675.     charindex('8.00', @@version) = 0)
  2676. begin
  2677.     print ''
  2678.     print ''
  2679.     print 'Warning:'
  2680.     print 'you are installing the stored procedures '
  2681.     print 'on a pre 6.0 SQL Server.'
  2682.     print 'Ignore the following errors.'
  2683. end
  2684. else
  2685.     drop proc sp_datatype_info
  2686. go
  2687.  
  2688. /*    Procedure for 6.0 and 6.50 servers */
  2689. create proc sp_datatype_info
  2690.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2691. as
  2692.     if @ODBCVer <> 3
  2693.         select @ODBCVer = 2
  2694.     if @data_type = 0
  2695.         select
  2696.             convert(varchar(32),case
  2697.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2698.                 else d.TYPE_NAME
  2699.             end) TYPE_NAME,
  2700.             d.DATA_TYPE,
  2701.             convert(int,case
  2702.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2703.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2704.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2705.                 else t.prec
  2706.             end) "PRECISION",
  2707.             d.LITERAL_PREFIX,
  2708.             d.LITERAL_SUFFIX,
  2709.             e.CREATE_PARAMS,
  2710.             convert(smallint,case
  2711.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2712.                 else t.allownulls
  2713.             end) NULLABLE,
  2714.             d.CASE_SENSITIVE,
  2715.             d.SEARCHABLE,
  2716.             d.UNSIGNED_ATTRIBUTE,
  2717.             d.MONEY,
  2718.             d.AUTO_INCREMENT,
  2719.             convert(varchar(32),case
  2720.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2721.                 else d.TYPE_NAME
  2722.             end) LOCAL_TYPE_NAME,
  2723.             convert(smallint,case
  2724.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2725.                 else d.numeric_scale
  2726.             end) MINIMUM_SCALE,
  2727.             convert(smallint,case
  2728.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2729.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2730.                 else t.scale
  2731.             end) MAXIMUM_SCALE,
  2732.             d.SQL_DATA_TYPE,
  2733.             d.SQL_DATETIME_SUB,
  2734.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2735.             INTERVAL_PRECISION = convert(smallint,NULL),
  2736.             USERTYPE = t.usertype
  2737.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2738.         where
  2739.             d.ss_dtype = t.type
  2740.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2741.             and t.usertype *= e.user_type
  2742.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2743.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2744.                 t.usertype > 100)
  2745.             and (t.usertype <= 100 or
  2746.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2747.         order by 2, 12, 11, t.usertype
  2748.  
  2749.     else
  2750.         select
  2751.             convert(varchar(32),case
  2752.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2753.                 else d.TYPE_NAME
  2754.             end) TYPE_NAME,
  2755.             d.DATA_TYPE,
  2756.             convert(int,case
  2757.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2758.                 when d.ss_dtype in (35,34) then 2147483647                /* TEXT/IMAGE */
  2759.                 when d.ss_dtype in (55,63,106,108) and t.usertype <= 100 then @@max_precision /* DECIMAL/NUMERIC */
  2760.                 else t.prec
  2761.             end) "PRECISION",
  2762.             d.LITERAL_PREFIX,
  2763.             d.LITERAL_SUFFIX,
  2764.             e.CREATE_PARAMS,
  2765.             convert(smallint,case
  2766.                 when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2767.                 else t.allownulls
  2768.             end) NULLABLE,
  2769.             d.CASE_SENSITIVE,
  2770.             d.SEARCHABLE,
  2771.             d.UNSIGNED_ATTRIBUTE,
  2772.             d.MONEY,
  2773.             d.AUTO_INCREMENT,
  2774.             convert(varchar(32),case
  2775.                 when t.usertype > 100 or t.usertype in (18,80) then t.name
  2776.                 else d.TYPE_NAME
  2777.             end) LOCAL_TYPE_NAME,
  2778.             convert(smallint,case
  2779.                 when d.ss_dtype in (55,63,106,108) and t.usertype > 100 then t.scale
  2780.                 else d.numeric_scale
  2781.             end) MINIMUM_SCALE,
  2782.             convert(smallint,case
  2783.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 0 then @@max_precision /* DECIMAL/NUMERIC */
  2784.                 when d.ss_dtype in (106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2785.                 else t.scale
  2786.             end) MAXIMUM_SCALE,
  2787.             d.SQL_DATA_TYPE,
  2788.             d.SQL_DATETIME_SUB,
  2789.             NUM_PREC_RADIX = convert(int,d.RADIX),
  2790.             INTERVAL_PRECISION = convert(smallint,NULL),
  2791.             USERTYPE = t.usertype
  2792.         from master.dbo.spt_datatype_info d, master.dbo.spt_datatype_info_ext e, systypes t
  2793.         where
  2794.             d.DATA_TYPE = @data_type
  2795.             and d.ss_dtype = t.type
  2796.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2797.             and t.usertype *= e.user_type
  2798.             and isnull(d.AUTO_INCREMENT,0) *= e.AUTO_INCREMENT
  2799.             and (t.type not in (111,109,38,110,55,63) or /* get rid of nullable types */
  2800.                 t.usertype > 100)
  2801.             and (t.usertype <= 100 or
  2802.                 isnull(d.AUTO_INCREMENT,0) = 0)
  2803.         order by 12, 11, t.usertype
  2804. go
  2805.  
  2806. if (charindex('7.00', @@version) = 0 and
  2807.     charindex('8.00', @@version) = 0)
  2808. begin
  2809.     print ''
  2810.     print ''
  2811.     print 'Warning:'
  2812.     print 'you are installing the stored procedures '
  2813.     print 'on a pre 7.0 SQL Server.'
  2814.     print 'Ignore the following errors.'
  2815. end
  2816. else
  2817.     drop proc sp_datatype_info
  2818. go
  2819.  
  2820. /*    Procedure for 7.0 server */
  2821. create proc sp_datatype_info
  2822.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2823. as
  2824.     declare @mintype int
  2825.     declare @maxtype int
  2826.  
  2827.     if @ODBCVer <> 3
  2828.         select @ODBCVer = 2
  2829.     if @data_type = 0
  2830.     begin
  2831.         select @mintype = -32768
  2832.         select @maxtype = 32767
  2833.     end
  2834.     else
  2835.     begin
  2836.         select @mintype = @data_type
  2837.         select @maxtype = @data_type
  2838.     end
  2839.  
  2840.     select
  2841.         convert(sysname,case
  2842.             when t.xusertype > 255 then t.name
  2843.             else d.TYPE_NAME
  2844.         end) TYPE_NAME,
  2845.         d.DATA_TYPE,
  2846.         convert(int,case
  2847.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  2848.             when type_name(d.ss_dtype) IN ('numeric','decimal') and
  2849.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2850.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2851.         end) "PRECISION",
  2852.         d.LITERAL_PREFIX,
  2853.         d.LITERAL_SUFFIX,
  2854.         e.CREATE_PARAMS,
  2855.         convert(smallint,case
  2856.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2857.             else TypeProperty (t.name, 'AllowsNull')
  2858.         end) NULLABLE,
  2859.         d.CASE_SENSITIVE,
  2860.         d.SEARCHABLE,
  2861.         d.UNSIGNED_ATTRIBUTE,
  2862.         d.MONEY,
  2863.         d.AUTO_INCREMENT,
  2864.         convert(sysname,case
  2865.             when t.xusertype > 255 then t.name
  2866.             else d.LOCAL_TYPE_NAME
  2867.         end) LOCAL_TYPE_NAME,
  2868.         MINIMUM_SCALE = d.numeric_scale,
  2869.         convert(smallint,case
  2870.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2871.             when type_name(d.ss_dtype) IN ('numeric','decimal') and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2872.             else TypeProperty (t.name, 'Scale')
  2873.         end) MAXIMUM_SCALE,
  2874.         d.SQL_DATA_TYPE,
  2875.         d.SQL_DATETIME_SUB,
  2876.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2877.         INTERVAL_PRECISION = convert(smallint,NULL),
  2878.         USERTYPE = t.usertype
  2879.     from master.dbo.spt_datatype_info d
  2880.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2881.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2882.             t.xusertype = e.user_type
  2883.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2884.     where
  2885.         d.DATA_TYPE between @mintype and @maxtype
  2886.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2887.         and (t.xusertype <= 255 or
  2888.             isnull(d.AUTO_INCREMENT,0) = 0)
  2889.     order by 2, 12, 11,
  2890.     case
  2891.         when t.usertype=18 then 255
  2892.         else t.usertype
  2893.     end
  2894.  
  2895. go
  2896.  
  2897. if (charindex('8.00', @@version) = 0)
  2898. begin
  2899.     print ''
  2900.     print ''
  2901.     print 'Warning:'
  2902.     print 'you are installing the stored procedures '
  2903.     print 'on a pre 8.0 SQL Server.'
  2904.     print 'Ignore the following errors.'
  2905. end
  2906. else
  2907.     drop proc sp_datatype_info
  2908. go
  2909.  
  2910. /*    Procedure for 8.0 server */
  2911. create proc sp_datatype_info
  2912.     (@data_type int = 0, @ODBCVer tinyint = 2)
  2913. as
  2914.     declare @mintype int
  2915.     declare @maxtype int
  2916.  
  2917.     if @ODBCVer <> 3
  2918.         select @ODBCVer = 2
  2919.     if @data_type = 0
  2920.     begin
  2921.         select @mintype = -32768
  2922.         select @maxtype = 32767
  2923.     end
  2924.     else
  2925.     begin
  2926.         select @mintype = @data_type
  2927.         select @maxtype = @data_type
  2928.     end
  2929.  
  2930.     select
  2931.         convert(sysname,case
  2932.             when t.xusertype > 255 then t.name
  2933.             else d.TYPE_NAME collate database_default
  2934.         end) TYPE_NAME,
  2935.         d.DATA_TYPE,
  2936.         convert(int,case
  2937.             when d.DATA_TYPE in (6,7,-150) then d.data_precision         /* FLOAT/REAL/sql_variant*/
  2938.             when d.ss_dtype in (55,63,106,108) and
  2939.                 t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2940.             else OdbcPrec(t.xtype, t.length, t.xprec)
  2941.         end) "PRECISION",
  2942.         d.LITERAL_PREFIX,
  2943.         d.LITERAL_SUFFIX,
  2944.         e.CREATE_PARAMS,
  2945.         convert(smallint,case
  2946.             when d.AUTO_INCREMENT = 1 then 0 /* IDENTITY*/
  2947.             else TypeProperty (t.name, 'AllowsNull')
  2948.         end) NULLABLE,
  2949.         d.CASE_SENSITIVE,
  2950.         d.SEARCHABLE,
  2951.         d.UNSIGNED_ATTRIBUTE,
  2952.         d.MONEY,
  2953.         d.AUTO_INCREMENT,
  2954.         convert(sysname,case
  2955.             when t.xusertype > 255 then t.name
  2956.             else d.LOCAL_TYPE_NAME collate database_default
  2957.         end) LOCAL_TYPE_NAME,
  2958.         convert(smallint,case
  2959.             when d.ss_dtype in (55,63,106,108) and t.xusertype > 255 then TypeProperty (t.name, 'Scale')
  2960.             else d.numeric_scale
  2961.         end) MINIMUM_SCALE,
  2962.         convert(smallint,case
  2963.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 0 and t.xusertype <= 255 then @@max_precision /* DECIMAL/NUMERIC */
  2964.             when d.ss_dtype in (55,63,106,108) and d.AUTO_INCREMENT = 1 then 0 /* DECIMAL/NUMERIC IDENTITY*/
  2965.             else TypeProperty (t.name, 'Scale')
  2966.         end) MAXIMUM_SCALE,
  2967.         d.SQL_DATA_TYPE,
  2968.         d.SQL_DATETIME_SUB,
  2969.         NUM_PREC_RADIX = convert(int,d.RADIX),
  2970.         INTERVAL_PRECISION = convert(smallint,NULL),
  2971.         USERTYPE = t.usertype
  2972.     from master.dbo.spt_datatype_info d
  2973.         INNER JOIN systypes t on d.ss_dtype = t.xtype
  2974.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  2975.             t.xusertype = e.user_type
  2976.             and isnull(d.AUTO_INCREMENT,0) = e.AUTO_INCREMENT
  2977.     where
  2978.         d.DATA_TYPE between @mintype and @maxtype
  2979.         and (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  2980.         and (t.xusertype <= 255 or
  2981.             isnull(d.AUTO_INCREMENT,0) = 0)
  2982.     order by 2, 12, 11,
  2983.     case
  2984.         when t.usertype=18 then 255
  2985.         else t.usertype
  2986.     end
  2987.  
  2988. go
  2989.  
  2990. grant execute on sp_datatype_info to public
  2991. go
  2992.  
  2993. dump tran master with no_log
  2994. go
  2995.  
  2996. print 'creating sp_fkeys'
  2997. go
  2998.  
  2999. /*    Procedure for pre-6.0 server */
  3000. CREATE PROCEDURE sp_fkeys(
  3001.                @pktable_name        varchar(32) = null,
  3002.                @pktable_owner        varchar(32) = null,
  3003.                @pktable_qualifier    varchar(32) = null,
  3004.                @fktable_name        varchar(32) = null,
  3005.                @fktable_owner        varchar(32) = null,
  3006.                @fktable_qualifier    varchar(32) = null )
  3007. as
  3008.     set nocount on
  3009.     declare    @order_by_pk int
  3010.  
  3011.     select  @order_by_pk = 0
  3012.  
  3013.     if (@pktable_name is null) and (@fktable_name is null)
  3014.     begin    /* If neither primary key nor foreign key table names given */
  3015.         raiserror 20004 '~~Rush_25~~'
  3016.         return
  3017.     end
  3018.     if @fktable_qualifier is not null
  3019.     begin
  3020.         if db_name() <> @fktable_qualifier
  3021.         begin    /* If qualifier doesn't match current database */
  3022.             raiserror 20001 '~~Rush_26~~'
  3023.             return
  3024.         end
  3025.     end
  3026.     if @pktable_qualifier is not null
  3027.     begin
  3028.         if db_name() <> @pktable_qualifier
  3029.         begin    /* If qualifier doesn't match current database */
  3030.             raiserror 20001 '~~Rush_27~~'
  3031.             return
  3032.         end
  3033.     end
  3034.  
  3035.     if @pktable_name is null
  3036.     begin /*  If table name not supplied, match all */
  3037.         select @pktable_name = '%'
  3038.         select @order_by_pk = 1
  3039.     end
  3040.     if @pktable_owner is null    /*    If PK owner not supplied, match all */
  3041.         select @pktable_owner = '%'
  3042.     if @fktable_name is null    /*    If table name not supplied, match all */
  3043.         select @fktable_name = '%'
  3044.     if @fktable_owner is null    /*    If FK owner not supplied, match all */
  3045.         select @fktable_owner = '%'
  3046.  
  3047.     if @@trancount <> 0
  3048.     begin    /* If inside a transaction */
  3049.         raiserror 20003 '~~Rush_28~~'
  3050.         return
  3051.     end
  3052.     create table #fkeys(
  3053.              PKTABLE_QUALIFIER    varchar(32) NULL,
  3054.              PKTABLE_OWNER        varchar(32) NULL,
  3055.              PKTABLE_NAME        varchar(32) NOT NULL,
  3056.              PKCOLUMN_NAME        varchar(32) NOT NULL,
  3057.              FKTABLE_QUALIFIER    varchar(32) NULL,
  3058.              FKTABLE_OWNER        varchar(32) NULL,
  3059.              FKTABLE_NAME        varchar(32) NOT NULL,
  3060.              FKCOLUMN_NAME        varchar(32) NOT NULL,
  3061.              KEY_SEQ            smallint NOT NULL)
  3062.  
  3063.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  3064.     /*    Process syskeys for each relationship */
  3065.     /*    The inserts below adds a row to the temp table for each of the
  3066.         8 possible relationships */
  3067.     insert into #fkeys
  3068.         select
  3069.             db_name(),
  3070.             user_name(o1.uid),
  3071.             object_name(k.depid),
  3072.             c2.name,
  3073.             db_name(),
  3074.             user_name(o2.uid),
  3075.             object_name(k.id),
  3076.             c1.name,
  3077.             1
  3078.         from
  3079.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3080.         where
  3081.             c1.id = k.id
  3082.             and k.type = 2    /* Foreign type key */
  3083.             and c1.colid = k.key1
  3084.             and c2.id = k.depid
  3085.             and c2.colid = k.depkey1
  3086.             and o1.id = k.depid
  3087.             and o2.id = k.id
  3088.     union all
  3089.         select
  3090.             db_name(),
  3091.             user_name(o1.uid),
  3092.             object_name(k.depid),
  3093.             c2.name,
  3094.             db_name(),
  3095.             user_name(o2.uid),
  3096.             object_name(k.id),
  3097.             c1.name,
  3098.             2
  3099.         from
  3100.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3101.         where
  3102.             c1.id = k.id
  3103.             and k.type = 2    /* Foreign type key */
  3104.             and c1.colid = k.key2
  3105.             and c2.id = k.depid
  3106.             and c2.colid = k.depkey2
  3107.             and o1.id = k.depid
  3108.             and o2.id = k.id
  3109.     union all
  3110.         select
  3111.             db_name(),
  3112.             user_name(o1.uid),
  3113.             object_name(k.depid),
  3114.             c2.name,
  3115.             db_name(),
  3116.             user_name(o2.uid),
  3117.             object_name(k.id),
  3118.             c1.name,
  3119.             3
  3120.         from
  3121.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3122.         where
  3123.             c1.id = k.id
  3124.             and k.type = 2    /* Foreign type key */
  3125.             and c1.colid = k.key3
  3126.             and c2.id = k.depid
  3127.             and c2.colid = k.depkey3
  3128.             and o1.id = k.depid
  3129.             and o2.id = k.id
  3130.     union all
  3131.         select
  3132.             db_name(),
  3133.             user_name(o1.uid),
  3134.             object_name(k.depid),
  3135.             c2.name,
  3136.             db_name(),
  3137.             user_name(o2.uid),
  3138.             object_name(k.id),
  3139.             c1.name,
  3140.             4
  3141.         from
  3142.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3143.         where
  3144.             c1.id = k.id
  3145.             and k.type = 2    /* Foreign type key */
  3146.             and c1.colid = k.key4
  3147.             and c2.id = k.depid
  3148.             and c2.colid = k.depkey4
  3149.             and o1.id = k.depid
  3150.             and o2.id = k.id
  3151.     union all
  3152.         select
  3153.             db_name(),
  3154.             user_name(o1.uid),
  3155.             object_name(k.depid),
  3156.             c2.name,
  3157.             db_name(),
  3158.             user_name(o2.uid),
  3159.             object_name(k.id),
  3160.             c1.name,
  3161.             5
  3162.         from
  3163.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3164.         where
  3165.             c1.id = k.id
  3166.             and k.type = 2    /* Foreign type key */
  3167.             and c1.colid = k.key5
  3168.             and c2.id = k.depid
  3169.             and c2.colid = k.depkey5
  3170.             and o1.id = k.depid
  3171.             and o2.id = k.id
  3172.     union all
  3173.         select
  3174.             db_name(),
  3175.             user_name(o1.uid),
  3176.             object_name(k.depid),
  3177.             c2.name,
  3178.             db_name(),
  3179.             user_name(o2.uid),
  3180.             object_name(k.id),
  3181.             c1.name,
  3182.             6
  3183.         from
  3184.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3185.         where
  3186.             c1.id = k.id
  3187.             and k.type = 2    /* Foreign type key */
  3188.             and c1.colid = k.key6
  3189.             and c2.id = k.depid
  3190.             and c2.colid = k.depkey6
  3191.             and o1.id = k.depid
  3192.             and o2.id = k.id
  3193.     union all
  3194.         select
  3195.             db_name(),
  3196.             user_name(o1.uid),
  3197.             object_name(k.depid),
  3198.             c2.name,
  3199.             db_name(),
  3200.             user_name(o2.uid),
  3201.             object_name(k.id),
  3202.             c1.name,
  3203.             7
  3204.         from
  3205.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3206.         where
  3207.             c1.id = k.id
  3208.             and k.type = 2    /* Foreign type key */
  3209.             and c1.colid = k.key7
  3210.             and c2.id = k.depid
  3211.             and c2.colid = k.depkey7
  3212.             and o1.id = k.depid
  3213.             and o2.id = k.id
  3214.     union all
  3215.         select
  3216.             db_name(),
  3217.             user_name(o1.uid),
  3218.             object_name(k.depid),
  3219.             c2.name,
  3220.             db_name(),
  3221.             user_name(o2.uid),
  3222.             object_name(k.id),
  3223.             c1.name,
  3224.             8
  3225.         from
  3226.             syskeys k, syscolumns c1, syscolumns c2,sysobjects o1, sysobjects o2
  3227.         where
  3228.             c1.id = k.id
  3229.             and k.type = 2    /* Foreign type key */
  3230.             and c1.colid = k.key8
  3231.             and c2.id = k.depid
  3232.             and c2.colid = k.depkey8
  3233.             and o1.id = k.depid
  3234.             and o2.id = k.id
  3235.  
  3236.     if @order_by_pk = 1 /*    If order by PK fields */
  3237.         select
  3238.             PKTABLE_QUALIFIER,
  3239.             PKTABLE_OWNER,
  3240.             PKTABLE_NAME,
  3241.             PKCOLUMN_NAME,
  3242.             FKTABLE_QUALIFIER,
  3243.             FKTABLE_OWNER,
  3244.             FKTABLE_NAME,
  3245.             FKCOLUMN_NAME,
  3246.             KEY_SEQ,
  3247.             UPDATE_RULE = convert(smallint, null),
  3248.             DELETE_RULE = convert(smallint,null),
  3249.             FK_NAME = convert(varchar(32),null),
  3250.             PK_NAME = convert(varchar(32),null),
  3251.             DEFERRABILITY = convert(smallint,7)
  3252.         from #fkeys
  3253.         where FKTABLE_NAME like @fktable_name
  3254.             and FKTABLE_OWNER like @fktable_owner
  3255.             and PKTABLE_NAME  like @pktable_name
  3256.             and PKTABLE_OWNER like @pktable_owner
  3257.         order by 1, 2, 3, 9, 4
  3258.     else        /*    Order by FK fields */
  3259.         select
  3260.             PKTABLE_QUALIFIER,
  3261.             PKTABLE_OWNER,
  3262.             PKTABLE_NAME,
  3263.             PKCOLUMN_NAME,
  3264.             FKTABLE_QUALIFIER,
  3265.             FKTABLE_OWNER,
  3266.             FKTABLE_NAME,
  3267.             FKCOLUMN_NAME,
  3268.             KEY_SEQ,
  3269.             UPDATE_RULE = convert(smallint,null),
  3270.             DELETE_RULE = convert(smallint,null),
  3271.             FK_NAME = convert(varchar(32),null),
  3272.             PK_NAME = convert(varchar(32),null),
  3273.             DEFERRABILITY = convert(smallint,7)
  3274.         from #fkeys
  3275.         where FKTABLE_NAME like @fktable_name
  3276.             and FKTABLE_OWNER like @fktable_owner
  3277.             and PKTABLE_NAME  like @pktable_name
  3278.             and PKTABLE_OWNER like @pktable_owner
  3279.         order by 5, 6, 7, 9, 8
  3280. go
  3281.  
  3282. if (charindex('6.00', @@version) = 0 and
  3283.     charindex('6.50', @@version) = 0 and
  3284.     charindex('7.00', @@version) = 0 and
  3285.     charindex('8.00', @@version) = 0)
  3286. begin
  3287.     print ''
  3288.     print ''
  3289.     print 'Warning:'
  3290.     print 'you are installing the stored procedures '
  3291.     print 'on a pre 6.0 SQL Server.'
  3292.     print 'Ignore the following errors.'
  3293. end
  3294. else
  3295.     drop proc sp_fkeys
  3296. go
  3297.  
  3298. /*    Procedure for 6.0 and 6.50 servers */
  3299. CREATE PROCEDURE sp_fkeys(
  3300.                @pktable_name        varchar(32) = null,
  3301.                @pktable_owner        varchar(32) = null,
  3302.                @pktable_qualifier    varchar(32) = null,
  3303.                @fktable_name        varchar(32) = null,
  3304.                @fktable_owner        varchar(32) = null,
  3305.                @fktable_qualifier    varchar(32) = null )
  3306. as
  3307.     set nocount on
  3308.     DECLARE @pktable_id            int
  3309.     DECLARE @pkfull_table_name    varchar(65) /* 2*32 +1 */
  3310.     DECLARE @fktable_id            int
  3311.     DECLARE @fkfull_table_name    varchar(65) /* 2*32 +1 */
  3312.     declare    @order_by_pk        int
  3313.  
  3314.     select  @order_by_pk = 0
  3315.  
  3316.     if (@pktable_name is null) and (@fktable_name is null)
  3317.     begin    /* If neither primary key nor foreign key table names given */
  3318.         raiserror (15252,-1,-1)
  3319.         return
  3320.     end
  3321.     if @fktable_qualifier is not null
  3322.     begin
  3323.         if db_name() <> @fktable_qualifier
  3324.         begin    /* If qualifier doesn't match current database */
  3325.             raiserror (15250, -1,-1)
  3326.             return
  3327.         end
  3328.     end
  3329.     if @pktable_qualifier is not null
  3330.     begin
  3331.         if db_name() <> @pktable_qualifier
  3332.         begin    /* If qualifier doesn't match current database */
  3333.             raiserror (15250, -1,-1)
  3334.             return
  3335.         end
  3336.     end
  3337.  
  3338.     if @pktable_owner is null
  3339.     begin    /* If unqualified primary key table name */
  3340.         SELECT @pkfull_table_name = @pktable_name
  3341.     end
  3342.     else
  3343.     begin    /* Qualified primary key table name */
  3344.         SELECT @pkfull_table_name = @pktable_owner + '.' + @pktable_name
  3345.     end
  3346.     /*    Get Object ID */
  3347.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3348.  
  3349.     if @fktable_owner is null
  3350.     begin    /* If unqualified foreign key table name */
  3351.         SELECT @fkfull_table_name = @fktable_name
  3352.     end
  3353.     else
  3354.     begin    /* Qualified foreign key table name */
  3355.         SELECT @fkfull_table_name = @fktable_owner + '.' + @fktable_name
  3356.     end
  3357.     /*    Get Object ID */
  3358.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3359.  
  3360.     if @fktable_name is not null
  3361.     begin
  3362.         if @fktable_id is null
  3363.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3364.     end
  3365.  
  3366.     if @pktable_name is null
  3367.     begin /*  If table name not supplied, match all */
  3368.         select @order_by_pk = 1
  3369.     end
  3370.     else
  3371.     begin
  3372.         if @pktable_id is null
  3373.         begin
  3374.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3375.         end
  3376.     end
  3377.  
  3378.     if (@@trancount <> 0 and
  3379.         charindex('6.50', @@version) = 0)
  3380.     begin    /* If inside a transaction */
  3381.         raiserror (15002,-1,-1,'sp_fkeys')
  3382.         return
  3383.     end
  3384.  
  3385.     create table #fkeys(
  3386.              pkdb_id        int NOT NULL,
  3387.              pktable_id     int NOT NULL,
  3388.              pkcolid        int NOT NULL,
  3389.              fkdb_id        int NOT NULL,
  3390.              fktable_id        int NOT NULL,
  3391.              fkcolid        int NOT NULL,
  3392.              KEY_SEQ        smallint NOT NULL,
  3393.              fk_id            int NOT NULL,
  3394.              pk_id            int NOT NULL)
  3395.  
  3396.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3397.     /*    Process syskeys for each relationship */
  3398.     /*    The inserts below adds a row to the temp table for each of the
  3399.         16 possible relationships */
  3400.     insert into #fkeys
  3401.         select
  3402.             r.rkeydbid,
  3403.             r.rkeyid,
  3404.             r.rkey1,
  3405.             r.fkeydbid,
  3406.             r.fkeyid,
  3407.             r.fkey1,
  3408.             1,
  3409.             r.constid,
  3410.             s.constid
  3411.         from
  3412.             sysreferences r, sysconstraints s
  3413.         where    r.rkeyid = s.id
  3414.             AND (s.status & 0xf) = 1
  3415.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3416.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3417.       union all
  3418.         select
  3419.             r.rkeydbid,
  3420.             r.rkeyid,
  3421.             r.rkey2,
  3422.             r.fkeydbid,
  3423.             r.fkeyid,
  3424.             r.fkey2,
  3425.             2,
  3426.             r.constid,
  3427.             s.constid
  3428.         from
  3429.             sysreferences r, sysconstraints s
  3430.         where    r.rkeyid = s.id
  3431.             AND (s.status & 0xf) = 1
  3432.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3433.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3434.       union all
  3435.         select
  3436.             r.rkeydbid,
  3437.             r.rkeyid,
  3438.             r.rkey3,
  3439.             r.fkeydbid,
  3440.             r.fkeyid,
  3441.             r.fkey3,
  3442.             3,
  3443.             r.constid,
  3444.             s.constid
  3445.         from
  3446.             sysreferences r, sysconstraints s
  3447.         where    r.rkeyid = s.id
  3448.             AND (s.status & 0xf) = 1
  3449.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3450.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3451.       union all
  3452.         select
  3453.             r.rkeydbid,
  3454.             r.rkeyid,
  3455.             r.rkey4,
  3456.             r.fkeydbid,
  3457.             r.fkeyid,
  3458.             r.fkey4,
  3459.             4,
  3460.             r.constid,
  3461.             s.constid
  3462.         from
  3463.             sysreferences r, sysconstraints s
  3464.         where    r.rkeyid = s.id
  3465.             AND (s.status & 0xf) = 1
  3466.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3467.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3468.       union all
  3469.         select
  3470.             r.rkeydbid,
  3471.             r.rkeyid,
  3472.             r.rkey5,
  3473.             r.fkeydbid,
  3474.             r.fkeyid,
  3475.             r.fkey5,
  3476.             5,
  3477.             r.constid,
  3478.             s.constid
  3479.         from
  3480.             sysreferences r, sysconstraints s
  3481.         where    r.rkeyid = s.id
  3482.             AND (s.status & 0xf) = 1
  3483.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3484.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3485.       union all
  3486.         select
  3487.             r.rkeydbid,
  3488.             r.rkeyid,
  3489.             r.rkey6,
  3490.             r.fkeydbid,
  3491.             r.fkeyid,
  3492.             r.fkey6,
  3493.             6,
  3494.             r.constid,
  3495.             s.constid
  3496.         from
  3497.             sysreferences r, sysconstraints s
  3498.         where    r.rkeyid = s.id
  3499.             AND (s.status & 0xf) = 1
  3500.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3501.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3502.       union all
  3503.         select
  3504.             r.rkeydbid,
  3505.             r.rkeyid,
  3506.             r.rkey7,
  3507.             r.fkeydbid,
  3508.             r.fkeyid,
  3509.             r.fkey7,
  3510.             7,
  3511.             r.constid,
  3512.             s.constid
  3513.         from
  3514.             sysreferences r, sysconstraints s
  3515.         where    r.rkeyid = s.id
  3516.             AND (s.status & 0xf) = 1
  3517.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3518.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3519.       union all
  3520.         select
  3521.             r.rkeydbid,
  3522.             r.rkeyid,
  3523.             r.rkey8,
  3524.             r.fkeydbid,
  3525.             r.fkeyid,
  3526.             r.fkey8,
  3527.             8,
  3528.             r.constid,
  3529.             s.constid
  3530.         from
  3531.             sysreferences r, sysconstraints s
  3532.         where    r.rkeyid = s.id
  3533.             AND (s.status & 0xf) = 1
  3534.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3535.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3536.       union all
  3537.         select
  3538.             r.rkeydbid,
  3539.             r.rkeyid,
  3540.             r.rkey9,
  3541.             r.fkeydbid,
  3542.             r.fkeyid,
  3543.             r.fkey9,
  3544.             9,
  3545.             r.constid,
  3546.             s.constid
  3547.         from
  3548.             sysreferences r, sysconstraints s
  3549.         where    r.rkeyid = s.id
  3550.             AND (s.status & 0xf) = 1
  3551.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3552.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3553.       union all
  3554.         select
  3555.             r.rkeydbid,
  3556.             r.rkeyid,
  3557.             r.rkey10,
  3558.             r.fkeydbid,
  3559.             r.fkeyid,
  3560.             r.fkey10,
  3561.             10,
  3562.             r.constid,
  3563.             s.constid
  3564.         from
  3565.             sysreferences r, sysconstraints s
  3566.         where    r.rkeyid = s.id
  3567.             AND (s.status & 0xf) = 1
  3568.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3569.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3570.       union all
  3571.         select
  3572.             r.rkeydbid,
  3573.             r.rkeyid,
  3574.             r.rkey11,
  3575.             r.fkeydbid,
  3576.             r.fkeyid,
  3577.             r.fkey11,
  3578.             11,
  3579.             r.constid,
  3580.             s.constid
  3581.         from
  3582.             sysreferences r, sysconstraints s
  3583.         where    r.rkeyid = s.id
  3584.             AND (s.status & 0xf) = 1
  3585.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3586.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3587.       union all
  3588.         select
  3589.             r.rkeydbid,
  3590.             r.rkeyid,
  3591.             r.rkey12,
  3592.             r.fkeydbid,
  3593.             r.fkeyid,
  3594.             r.fkey12,
  3595.             12,
  3596.             r.constid,
  3597.             s.constid
  3598.         from
  3599.             sysreferences r, sysconstraints s
  3600.         where    r.rkeyid = s.id
  3601.             AND (s.status & 0xf) = 1
  3602.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3603.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3604.       union all
  3605.         select
  3606.             r.rkeydbid,
  3607.             r.rkeyid,
  3608.             r.rkey13,
  3609.             r.fkeydbid,
  3610.             r.fkeyid,
  3611.             r.fkey13,
  3612.             13,
  3613.             r.constid,
  3614.             s.constid
  3615.         from
  3616.             sysreferences r, sysconstraints s
  3617.         where    r.rkeyid = s.id
  3618.             AND (s.status & 0xf) = 1
  3619.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3620.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3621.       union all
  3622.         select
  3623.             r.rkeydbid,
  3624.             r.rkeyid,
  3625.             r.rkey14,
  3626.             r.fkeydbid,
  3627.             r.fkeyid,
  3628.             r.fkey14,
  3629.             14,
  3630.             r.constid,
  3631.             s.constid
  3632.         from
  3633.             sysreferences r, sysconstraints s
  3634.         where    r.rkeyid = s.id
  3635.             AND (s.status & 0xf) = 1
  3636.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3637.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3638.       union all
  3639.         select
  3640.             r.rkeydbid,
  3641.             r.rkeyid,
  3642.             r.rkey15,
  3643.             r.fkeydbid,
  3644.             r.fkeyid,
  3645.             r.fkey15,
  3646.             15,
  3647.             r.constid,
  3648.             s.constid
  3649.         from
  3650.             sysreferences r, sysconstraints s
  3651.         where    r.rkeyid = s.id
  3652.             AND (s.status & 0xf) = 1
  3653.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3654.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3655.       union all
  3656.         select
  3657.             r.rkeydbid,
  3658.             r.rkeyid,
  3659.             r.rkey16,
  3660.             r.fkeydbid,
  3661.             r.fkeyid,
  3662.             r.fkey16,
  3663.             16,
  3664.             r.constid,
  3665.             s.constid
  3666.         from
  3667.             sysreferences r, sysconstraints s
  3668.         where    r.rkeyid = s.id
  3669.             AND (s.status & 0xf) = 1
  3670.             AND r.rkeyid between isnull(@pktable_id, 0) and isnull(@pktable_id, 0x7fffffff)
  3671.             AND r.fkeyid between isnull(@fktable_id, 0) and isnull(@fktable_id, 0x7fffffff)
  3672.  
  3673.     if @order_by_pk = 1 /*    If order by PK fields */
  3674.         select
  3675.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3676.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3677.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3678.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3679.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3680.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3681.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3682.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3683.             KEY_SEQ,
  3684.             UPDATE_RULE = convert(smallint,1),
  3685.             DELETE_RULE = convert(smallint,1),
  3686.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3687.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3688.             DEFERRABILITY = convert(smallint,7)
  3689.         from #fkeys f,
  3690.             sysobjects o1, sysobjects o2,
  3691.             syscolumns c1, syscolumns c2
  3692.         where    o1.id = f.pktable_id
  3693.             AND o2.id = f.fktable_id
  3694.             AND c1.id = f.pktable_id
  3695.             AND c2.id = f.fktable_id
  3696.             AND c1.colid = f.pkcolid
  3697.             AND c2.colid = f.fkcolid
  3698.         order by 1,2,3,9,4
  3699.     else        /*    Order by FK fields */
  3700.         select
  3701.             PKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.pkdb_id)),
  3702.             PKTABLE_OWNER = convert(varchar(32),USER_NAME(o1.uid)),
  3703.             PKTABLE_NAME = convert(varchar(32),o1.name),
  3704.             PKCOLUMN_NAME = convert(varchar(32),c1.name),
  3705.             FKTABLE_QUALIFIER = convert(varchar(32),DB_NAME(f.fkdb_id)),
  3706.             FKTABLE_OWNER = convert(varchar(32),USER_NAME(o2.uid)),
  3707.             FKTABLE_NAME = convert(varchar(32),o2.name),
  3708.             FKCOLUMN_NAME = convert(varchar(32),c2.name),
  3709.             KEY_SEQ,
  3710.             UPDATE_RULE = convert(smallint,1),
  3711.             DELETE_RULE = convert(smallint,1),
  3712.             FK_NAME = convert(varchar(32),OBJECT_NAME(fk_id)),
  3713.             PK_NAME = convert(varchar(32),OBJECT_NAME(pk_id)),
  3714.             DEFERRABILITY = convert(smallint,7)
  3715.         from #fkeys f,
  3716.             sysobjects o1, sysobjects o2,
  3717.             syscolumns c1, syscolumns c2
  3718.         where    o1.id = f.pktable_id
  3719.             AND o2.id = f.fktable_id
  3720.             AND c1.id = f.pktable_id
  3721.             AND c2.id = f.fktable_id
  3722.             AND c1.colid = f.pkcolid
  3723.             AND c2.colid = f.fkcolid
  3724.         order by 5,6,7,9,8
  3725. go
  3726.  
  3727. if (charindex('7.00', @@version) = 0 and
  3728.     charindex('8.00', @@version) = 0)
  3729. begin
  3730.     print ''
  3731.     print ''
  3732.     print 'Warning:'
  3733.     print 'you are installing the stored procedures '
  3734.     print 'on a pre 7.0 SQL Server.'
  3735.     print 'Ignore the following errors.'
  3736. end
  3737. else
  3738.     drop proc sp_fkeys
  3739. go
  3740.  
  3741. /*    Procedure for 7.0 server */
  3742. CREATE PROCEDURE sp_fkeys(
  3743.                @pktable_name        sysname = null,
  3744.                @pktable_owner        sysname = null,
  3745.                @pktable_qualifier    sysname = null,
  3746.                @fktable_name        sysname = null,
  3747.                @fktable_owner        sysname = null,
  3748.                @fktable_qualifier    sysname = null )
  3749. as
  3750.     set nocount on
  3751.     DECLARE @pktable_id            int
  3752.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3753.     DECLARE @fktable_id            int
  3754.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  3755.     declare    @order_by_pk        int
  3756.  
  3757.     /* select 'XXX starting table creation' */
  3758.  
  3759.     create table #fkeysall(
  3760.             rkeyid int NOT NULL,
  3761.             rkey1 int NOT NULL,
  3762.                 rkey2 int NOT NULL,
  3763.                 rkey3 int NOT NULL,
  3764.                 rkey4 int NOT NULL,
  3765.                 rkey5 int NOT NULL,
  3766.                 rkey6 int NOT NULL,
  3767.                 rkey7 int NOT NULL,
  3768.                 rkey8 int NOT NULL,
  3769.                 rkey9 int NOT NULL,
  3770.                 rkey10 int NOT NULL,
  3771.                 rkey11 int NOT NULL,
  3772.                 rkey12 int NOT NULL,
  3773.                 rkey13 int NOT NULL,
  3774.                 rkey14 int NOT NULL,
  3775.                 rkey15 int NOT NULL,
  3776.                 rkey16 int NOT NULL,
  3777.             fkeyid int NOT NULL,
  3778.             fkey1 int NOT NULL,
  3779.                 fkey2 int NOT NULL,
  3780.                 fkey3 int NOT NULL,
  3781.                 fkey4 int NOT NULL,
  3782.                 fkey5 int NOT NULL,
  3783.                 fkey6 int NOT NULL,
  3784.                 fkey7 int NOT NULL,
  3785.                 fkey8 int NOT NULL,
  3786.                 fkey9 int NOT NULL,
  3787.                 fkey10 int NOT NULL,
  3788.                 fkey11 int NOT NULL,
  3789.                 fkey12 int NOT NULL,
  3790.                 fkey13 int NOT NULL,
  3791.                 fkey14 int NOT NULL,
  3792.                 fkey15 int NOT NULL,
  3793.                 fkey16 int NOT NULL,
  3794.             constid int NOT NULL,
  3795.             name sysname NOT NULL)
  3796.  
  3797.     create table #fkeys(
  3798.             pktable_id        int NOT NULL,
  3799.             pkcolid         int NOT NULL,
  3800.             fktable_id        int NOT NULL,
  3801.             fkcolid         int NOT NULL,
  3802.             KEY_SEQ         smallint NOT NULL,
  3803.             fk_id            int NOT NULL,
  3804.             PK_NAME            sysname NOT NULL)
  3805.  
  3806.     create table #fkeysout(
  3807.             PKTABLE_QUALIFIER sysname NULL,
  3808.             PKTABLE_OWNER sysname NULL,
  3809.             PKTABLE_NAME sysname NOT NULL,
  3810.             PKCOLUMN_NAME sysname NOT NULL,
  3811.             FKTABLE_QUALIFIER sysname NULL,
  3812.             FKTABLE_OWNER sysname NULL,
  3813.             FKTABLE_NAME sysname NOT NULL,
  3814.             FKCOLUMN_NAME sysname NOT NULL,
  3815.             KEY_SEQ smallint NOT NULL,
  3816.             UPDATE_RULE smallint NULL,
  3817.             DELETE_RULE smallint NULL,
  3818.             FK_NAME sysname NULL,
  3819.             PK_NAME sysname NULL,
  3820.             DEFERRABILITY smallint null)
  3821.  
  3822.     /* select 'XXX starting parameter analysis' */
  3823.  
  3824.     select  @order_by_pk = 0
  3825.  
  3826.     if (@pktable_name is null) and (@fktable_name is null)
  3827.     begin    /* If neither primary key nor foreign key table names given */
  3828.         raiserror (15252,-1,-1)
  3829.         return
  3830.     end
  3831.     if @fktable_qualifier is not null
  3832.     begin
  3833.         if db_name() <> @fktable_qualifier
  3834.         begin    /* If qualifier doesn't match current database */
  3835.             raiserror (15250, -1,-1)
  3836.             return
  3837.         end
  3838.     end
  3839.     if @pktable_qualifier is not null
  3840.     begin
  3841.         if db_name() <> @pktable_qualifier
  3842.         begin    /* If qualifier doesn't match current database */
  3843.             raiserror (15250, -1,-1)
  3844.             return
  3845.         end
  3846.     end
  3847.  
  3848.     if @pktable_owner is null
  3849.     begin    /* If unqualified primary key table name */
  3850.         SELECT @pkfull_table_name = quotename(@pktable_name)
  3851.     end
  3852.     else
  3853.     begin    /* Qualified primary key table name */
  3854.         if @pktable_owner = ''
  3855.         begin    /* If empty owner name */
  3856.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  3857.         end
  3858.         else
  3859.         begin
  3860.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  3861.                 '.' + quotename(@pktable_name)
  3862.         end
  3863.     end
  3864.     /*    Get Object ID */
  3865.     SELECT @pktable_id = object_id(@pkfull_table_name)
  3866.  
  3867.     if @fktable_owner is null
  3868.     begin    /* If unqualified foreign key table name */
  3869.         SELECT @fkfull_table_name = quotename(@fktable_name)
  3870.     end
  3871.     else
  3872.     begin    /* Qualified foreign key table name */
  3873.         if @fktable_owner = ''
  3874.         begin    /* If empty owner name */
  3875.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  3876.         end
  3877.         else
  3878.         begin
  3879.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  3880.                 '.' + quotename(@fktable_name)
  3881.         end
  3882.     end
  3883.     /*    Get Object ID */
  3884.     SELECT @fktable_id = object_id(@fkfull_table_name)
  3885.  
  3886.     if @fktable_name is not null
  3887.     begin
  3888.         if @fktable_id is null
  3889.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  3890.     end
  3891.  
  3892.     if @pktable_name is null
  3893.     begin /*  If table name not supplied, match all */
  3894.         select @order_by_pk = 1
  3895.     end
  3896.     else
  3897.     begin
  3898.         if @pktable_id is null
  3899.         begin
  3900.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  3901.         end
  3902.     end
  3903.  
  3904.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  3905.     /*    Process syskeys for each relationship */
  3906.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  3907.         them out with a 16-way "insert select ... union select ..." */
  3908.  
  3909.     /* select 'XXX starting data analysis' */
  3910.  
  3911.     insert into #fkeysall
  3912.         select
  3913.             r.rkeyid,
  3914.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  3915.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  3916.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  3917.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  3918.             r.fkeyid,
  3919.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  3920.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  3921.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  3922.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  3923.             r.constid,
  3924.             i.name
  3925.         from
  3926.             sysreferences r, sysobjects o, sysindexes i
  3927.         where    r.constid = o.id
  3928.             AND o.xtype = 'F'
  3929.             AND r.rkeyindid = i.indid
  3930.             AND r.rkeyid = i.id
  3931.             AND r.rkeyid between isnull(@pktable_id, 0)
  3932.                             and isnull(@pktable_id, 0x7fffffff)
  3933.             AND r.fkeyid between isnull(@fktable_id, 0)
  3934.                             and isnull(@fktable_id, 0x7fffffff)
  3935.  
  3936.     /* select count (*) as 'XXX countall' from #fkeysall */
  3937.  
  3938.     insert into #fkeys
  3939.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  3940.             from #fkeysall
  3941.         union all
  3942.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  3943.             from #fkeysall
  3944.         union all
  3945.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  3946.             from #fkeysall
  3947.         union all
  3948.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  3949.             from #fkeysall
  3950.         union all
  3951.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  3952.             from #fkeysall
  3953.         union all
  3954.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  3955.             from #fkeysall
  3956.         union all
  3957.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  3958.             from #fkeysall
  3959.         union all
  3960.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  3961.             from #fkeysall
  3962.         union all
  3963.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  3964.             from #fkeysall
  3965.         union all
  3966.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  3967.             from #fkeysall
  3968.         union all
  3969.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  3970.             from #fkeysall
  3971.         union all
  3972.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  3973.             from #fkeysall
  3974.         union all
  3975.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  3976.             from #fkeysall
  3977.         union all
  3978.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  3979.             from #fkeysall
  3980.         union all
  3981.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  3982.             from #fkeysall
  3983.         union all
  3984.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  3985.             from #fkeysall
  3986.  
  3987.     /* select count (*) as 'XXX count' from #fkeys */
  3988.  
  3989.     insert into #fkeysout
  3990.         select
  3991.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  3992.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  3993.             PKTABLE_NAME = convert(sysname,o1.name),
  3994.             PKCOLUMN_NAME = convert(sysname,c1.name),
  3995.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  3996.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  3997.             FKTABLE_NAME = convert(sysname,o2.name),
  3998.             FKCOLUMN_NAME = convert(sysname,c2.name),
  3999.             KEY_SEQ,
  4000.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  4001.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4002.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  4003.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4004.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  4005.             PK_NAME,
  4006.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  4007.         from #fkeys f,
  4008.             sysobjects o1, sysobjects o2,
  4009.             syscolumns c1, syscolumns c2
  4010.         where    o1.id = f.pktable_id
  4011.             AND o2.id = f.fktable_id
  4012.             AND c1.id = f.pktable_id
  4013.             AND c2.id = f.fktable_id
  4014.             AND c1.colid = f.pkcolid
  4015.             AND c2.colid = f.fkcolid
  4016.     /* select count (*) as 'XXX countout' from #fkeysout */
  4017.  
  4018.     if @order_by_pk = 1 /*    If order by PK fields */
  4019.         select
  4020.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4021.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4022.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4023.         from #fkeysout
  4024.         order by 1,2,3,9,4
  4025.     else        /*    Order by FK fields */
  4026.         select
  4027.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4028.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4029.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4030.         from #fkeysout
  4031.         order by 5,6,7,9,8
  4032. go
  4033.  
  4034. if (charindex('8.00', @@version) = 0)
  4035. begin
  4036.     print ''
  4037.     print ''
  4038.     print 'Warning:'
  4039.     print 'you are installing the stored procedures '
  4040.     print 'on a pre 8.0 SQL Server.'
  4041.     print 'Ignore the following errors.'
  4042. end
  4043. else
  4044.     drop proc sp_fkeys
  4045. go
  4046.  
  4047. /*    Procedure for 8.0 server */
  4048. CREATE PROCEDURE sp_fkeys(
  4049.                @pktable_name        sysname = null,
  4050.                @pktable_owner        sysname = null,
  4051.                @pktable_qualifier    sysname = null,
  4052.                @fktable_name        sysname = null,
  4053.                @fktable_owner        sysname = null,
  4054.                @fktable_qualifier    sysname = null )
  4055. as
  4056.     set nocount on
  4057.     DECLARE @pktable_id            int
  4058.     DECLARE @pkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  4059.     DECLARE @fktable_id            int
  4060.     DECLARE @fkfull_table_name    nvarchar(257) /* 2*128 + 1 */
  4061.     declare    @order_by_pk        int
  4062.  
  4063.     /* select 'XXX starting table creation' */
  4064.  
  4065.     create table #fkeysall(
  4066.             rkeyid int NOT NULL,
  4067.             rkey1 int NOT NULL,
  4068.                 rkey2 int NOT NULL,
  4069.                 rkey3 int NOT NULL,
  4070.                 rkey4 int NOT NULL,
  4071.                 rkey5 int NOT NULL,
  4072.                 rkey6 int NOT NULL,
  4073.                 rkey7 int NOT NULL,
  4074.                 rkey8 int NOT NULL,
  4075.                 rkey9 int NOT NULL,
  4076.                 rkey10 int NOT NULL,
  4077.                 rkey11 int NOT NULL,
  4078.                 rkey12 int NOT NULL,
  4079.                 rkey13 int NOT NULL,
  4080.                 rkey14 int NOT NULL,
  4081.                 rkey15 int NOT NULL,
  4082.                 rkey16 int NOT NULL,
  4083.             fkeyid int NOT NULL,
  4084.             fkey1 int NOT NULL,
  4085.                 fkey2 int NOT NULL,
  4086.                 fkey3 int NOT NULL,
  4087.                 fkey4 int NOT NULL,
  4088.                 fkey5 int NOT NULL,
  4089.                 fkey6 int NOT NULL,
  4090.                 fkey7 int NOT NULL,
  4091.                 fkey8 int NOT NULL,
  4092.                 fkey9 int NOT NULL,
  4093.                 fkey10 int NOT NULL,
  4094.                 fkey11 int NOT NULL,
  4095.                 fkey12 int NOT NULL,
  4096.                 fkey13 int NOT NULL,
  4097.                 fkey14 int NOT NULL,
  4098.                 fkey15 int NOT NULL,
  4099.                 fkey16 int NOT NULL,
  4100.             constid int NOT NULL,
  4101.             name sysname collate database_default NOT NULL)
  4102.  
  4103.     create table #fkeys(
  4104.             pktable_id        int NOT NULL,
  4105.             pkcolid         int NOT NULL,
  4106.             fktable_id        int NOT NULL,
  4107.             fkcolid         int NOT NULL,
  4108.             KEY_SEQ         smallint NOT NULL,
  4109.             fk_id            int NOT NULL,
  4110.             PK_NAME            sysname collate database_default NOT NULL)
  4111.  
  4112.     create table #fkeysout(
  4113.             PKTABLE_QUALIFIER sysname collate database_default NULL,
  4114.             PKTABLE_OWNER sysname collate database_default NULL,
  4115.             PKTABLE_NAME sysname collate database_default NOT NULL,
  4116.             PKCOLUMN_NAME sysname collate database_default NOT NULL,
  4117.             FKTABLE_QUALIFIER sysname collate database_default NULL,
  4118.             FKTABLE_OWNER sysname collate database_default NULL,
  4119.             FKTABLE_NAME sysname collate database_default NOT NULL,
  4120.             FKCOLUMN_NAME sysname collate database_default NOT NULL,
  4121.             KEY_SEQ smallint NOT NULL,
  4122.             UPDATE_RULE smallint NULL,
  4123.             DELETE_RULE smallint NULL,
  4124.             FK_NAME sysname collate database_default NULL,
  4125.             PK_NAME sysname collate database_default NULL,
  4126.             DEFERRABILITY smallint null)
  4127.  
  4128.     /* select 'XXX starting parameter analysis' */
  4129.  
  4130.     select  @order_by_pk = 0
  4131.  
  4132.     if (@pktable_name is null) and (@fktable_name is null)
  4133.     begin    /* If neither primary key nor foreign key table names given */
  4134.         raiserror (15252,-1,-1)
  4135.         return
  4136.     end
  4137.     if @fktable_qualifier is not null
  4138.     begin
  4139.         if db_name() <> @fktable_qualifier
  4140.         begin    /* If qualifier doesn't match current database */
  4141.             raiserror (15250, -1,-1)
  4142.             return
  4143.         end
  4144.     end
  4145.     if @pktable_qualifier is not null
  4146.     begin
  4147.         if db_name() <> @pktable_qualifier
  4148.         begin    /* If qualifier doesn't match current database */
  4149.             raiserror (15250, -1,-1)
  4150.             return
  4151.         end
  4152.     end
  4153.  
  4154.     if @pktable_owner is null
  4155.     begin    /* If unqualified primary key table name */
  4156.         SELECT @pkfull_table_name = quotename(@pktable_name)
  4157.     end
  4158.     else
  4159.     begin    /* Qualified primary key table name */
  4160.         if @pktable_owner = ''
  4161.         begin    /* If empty owner name */
  4162.             SELECT @pkfull_table_name = quotename(@pktable_owner)
  4163.         end
  4164.         else
  4165.         begin
  4166.             SELECT @pkfull_table_name = quotename(@pktable_owner) +
  4167.                 '.' + quotename(@pktable_name)
  4168.         end
  4169.     end
  4170.     /*    Get Object ID */
  4171.     SELECT @pktable_id = object_id(@pkfull_table_name)
  4172.  
  4173.     if @fktable_owner is null
  4174.     begin    /* If unqualified foreign key table name */
  4175.         SELECT @fkfull_table_name = quotename(@fktable_name)
  4176.     end
  4177.     else
  4178.     begin    /* Qualified foreign key table name */
  4179.         if @fktable_owner = ''
  4180.         begin    /* If empty owner name */
  4181.             SELECT @fkfull_table_name = quotename(@fktable_owner)
  4182.         end
  4183.         else
  4184.         begin
  4185.             SELECT @fkfull_table_name = quotename(@fktable_owner) +
  4186.                 '.' + quotename(@fktable_name)
  4187.         end
  4188.     end
  4189.     /*    Get Object ID */
  4190.     SELECT @fktable_id = object_id(@fkfull_table_name)
  4191.  
  4192.     if @fktable_name is not null
  4193.     begin
  4194.         if @fktable_id is null
  4195.             SELECT @fktable_id = 0    /* fk table not found, empty result */
  4196.     end
  4197.  
  4198.     if @pktable_name is null
  4199.     begin /*  If table name not supplied, match all */
  4200.         select @order_by_pk = 1
  4201.     end
  4202.     else
  4203.     begin
  4204.         if @pktable_id is null
  4205.         begin
  4206.             SELECT @pktable_id = 0    /* pk table not found, empty result */
  4207.         end
  4208.     end
  4209.  
  4210.     /*    SQL Server supports upto 16 PK/FK relationships between 2 tables */
  4211.     /*    Process syskeys for each relationship */
  4212.     /*  First, attempt to get all 16 keys for each rel'ship, then sort
  4213.         them out with a 16-way "insert select ... union select ..." */
  4214.  
  4215.     /* select 'XXX starting data analysis' */
  4216.  
  4217.     insert into #fkeysall
  4218.         select
  4219.             r.rkeyid,
  4220.             r.rkey1, r.rkey2, r.rkey3, r.rkey4,
  4221.                 r.rkey5, r.rkey6, r.rkey7, r.rkey8,
  4222.                 r.rkey9, r.rkey10, r.rkey11, r.rkey12,
  4223.                 r.rkey13, r.rkey14, r.rkey15, r.rkey16,
  4224.             r.fkeyid,
  4225.             r.fkey1, r.fkey2, r.fkey3, r.fkey4,
  4226.                 r.fkey5, r.fkey6, r.fkey7, r.fkey8,
  4227.                 r.fkey9, r.fkey10, r.fkey11, r.fkey12,
  4228.                 r.fkey13, r.fkey14, r.fkey15, r.fkey16,
  4229.             r.constid,
  4230.             i.name
  4231.         from
  4232.             sysreferences r, sysobjects o, sysindexes i
  4233.         where    r.constid = o.id
  4234.             AND o.xtype = 'F'
  4235.             AND r.rkeyindid = i.indid
  4236.             AND r.rkeyid = i.id
  4237.             AND r.rkeyid between isnull(@pktable_id, 0)
  4238.                             and isnull(@pktable_id, 0x7fffffff)
  4239.             AND r.fkeyid between isnull(@fktable_id, 0)
  4240.                             and isnull(@fktable_id, 0x7fffffff)
  4241.  
  4242.     /* select count (*) as 'XXX countall' from #fkeysall */
  4243.  
  4244.     insert into #fkeys
  4245.             select rkeyid, rkey1, fkeyid, fkey1, 1, constid, name
  4246.             from #fkeysall
  4247.         union all
  4248.             select rkeyid, rkey2, fkeyid, fkey2, 2, constid, name
  4249.             from #fkeysall
  4250.         union all
  4251.             select rkeyid, rkey3, fkeyid, fkey3, 3, constid, name
  4252.             from #fkeysall
  4253.         union all
  4254.             select rkeyid, rkey4, fkeyid, fkey4, 4, constid, name
  4255.             from #fkeysall
  4256.         union all
  4257.             select rkeyid, rkey5, fkeyid, fkey5, 5, constid, name
  4258.             from #fkeysall
  4259.         union all
  4260.             select rkeyid, rkey6, fkeyid, fkey6, 6, constid, name
  4261.             from #fkeysall
  4262.         union all
  4263.             select rkeyid, rkey7, fkeyid, fkey7, 7, constid, name
  4264.             from #fkeysall
  4265.         union all
  4266.             select rkeyid, rkey8, fkeyid, fkey8, 8, constid, name
  4267.             from #fkeysall
  4268.         union all
  4269.             select rkeyid, rkey9, fkeyid, fkey9, 9, constid, name
  4270.             from #fkeysall
  4271.         union all
  4272.             select rkeyid, rkey10, fkeyid, fkey10, 10, constid, name
  4273.             from #fkeysall
  4274.         union all
  4275.             select rkeyid, rkey11, fkeyid, fkey11, 11, constid, name
  4276.             from #fkeysall
  4277.         union all
  4278.             select rkeyid, rkey12, fkeyid, fkey12, 12, constid, name
  4279.             from #fkeysall
  4280.         union all
  4281.             select rkeyid, rkey13, fkeyid, fkey13, 13, constid, name
  4282.             from #fkeysall
  4283.         union all
  4284.             select rkeyid, rkey14, fkeyid, fkey14, 14, constid, name
  4285.             from #fkeysall
  4286.         union all
  4287.             select rkeyid, rkey15, fkeyid, fkey15, 15, constid, name
  4288.             from #fkeysall
  4289.         union all
  4290.             select rkeyid, rkey16, fkeyid, fkey16, 16, constid, name
  4291.             from #fkeysall
  4292.  
  4293.     /* select count (*) as 'XXX count' from #fkeys */
  4294.  
  4295.     insert into #fkeysout
  4296.         select
  4297.             PKTABLE_QUALIFIER = convert(sysname,db_name()),
  4298.             PKTABLE_OWNER = convert(sysname,USER_NAME(o1.uid)),
  4299.             PKTABLE_NAME = convert(sysname,o1.name),
  4300.             PKCOLUMN_NAME = convert(sysname,c1.name),
  4301.             FKTABLE_QUALIFIER = convert(sysname,db_name()),
  4302.             FKTABLE_OWNER = convert(sysname,USER_NAME(o2.uid)),
  4303.             FKTABLE_NAME = convert(sysname,o2.name),
  4304.             FKCOLUMN_NAME = convert(sysname,c2.name),
  4305.             KEY_SEQ,
  4306.             UPDATE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsUpdateCascade')=1) THEN 
  4307.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4308.             DELETE_RULE = CASE WHEN (ObjectProperty(fk_id, 'CnstIsDeleteCascade')=1) THEN 
  4309.                 convert(smallint,0) ELSE convert(smallint,1) END,
  4310.             FK_NAME = convert(sysname,OBJECT_NAME(fk_id)),
  4311.             PK_NAME,
  4312.             DEFERRABILITY = 7    /* SQL_NOT_DEFERRABLE */
  4313.         from #fkeys f,
  4314.             sysobjects o1, sysobjects o2,
  4315.             syscolumns c1, syscolumns c2
  4316.         where    o1.id = f.pktable_id
  4317.             AND o2.id = f.fktable_id
  4318.             AND c1.id = f.pktable_id
  4319.             AND c2.id = f.fktable_id
  4320.             AND c1.colid = f.pkcolid
  4321.             AND c2.colid = f.fkcolid
  4322.     /* select count (*) as 'XXX countout' from #fkeysout */
  4323.  
  4324.     if @order_by_pk = 1 /*    If order by PK fields */
  4325.         select
  4326.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4327.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4328.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4329.         from #fkeysout
  4330.         order by 1,2,3,9,4
  4331.     else        /*    Order by FK fields */
  4332.         select
  4333.             PKTABLE_QUALIFIER, PKTABLE_OWNER, PKTABLE_NAME, PKCOLUMN_NAME,
  4334.             FKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, FKCOLUMN_NAME,
  4335.             KEY_SEQ, UPDATE_RULE, DELETE_RULE, FK_NAME, PK_NAME, DEFERRABILITY
  4336.         from #fkeysout
  4337.         order by 5,6,7,9,8
  4338. go
  4339. grant execute on sp_fkeys to public
  4340. go
  4341.  
  4342. dump tran master with no_log
  4343. go
  4344.  
  4345. print 'creating sp_pkeys'
  4346. go
  4347.  
  4348. /*    Procedure for pre-6.0 server */
  4349. CREATE PROCEDURE sp_pkeys(
  4350.                @table_name        varchar(32),
  4351.                @table_owner     varchar(32) = null,
  4352.                @table_qualifier varchar(32) = null )
  4353. as
  4354.     set nocount on
  4355.     if @table_qualifier is not null
  4356.     begin
  4357.         if db_name() <> @table_qualifier
  4358.         begin    /* If qualifier doesn't match current database */
  4359.             raiserror 20001 '~~Rush_5~~'
  4360.             return
  4361.         end
  4362.     end
  4363.     if @table_owner is null /*    If owner not supplied, match all */
  4364.         select @table_owner = '%'
  4365.     if @@trancount <> 0
  4366.     begin    /* If inside a transaction */
  4367.         raiserror 20003 '~~Rush_35~~'
  4368.         return
  4369.     end
  4370.  
  4371.     create table #pkeys(
  4372.              TABLE_QUALIFIER varchar(32) NULL,
  4373.              TABLE_OWNER     varchar(32) NULL,
  4374.              TABLE_NAME      varchar(32) NOT NULL,
  4375.              COLUMN_NAME     varchar(32) NOT NULL,
  4376.              KEY_SEQ         smallint NOT NULL)
  4377.  
  4378.     /*    SQL Server supports upto 8 PK/FK relationships between 2 tables */
  4379.     /*    Process syskeys for each relationship */
  4380.     /*    The inserts below adds a row to the temp table for each of the
  4381.         8 possible relationships */
  4382.     insert into #pkeys
  4383.         select
  4384.             db_name(),
  4385.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4386.             object_name(k.id),
  4387.             c.name,
  4388.             1
  4389.         from
  4390.             syskeys k, syscolumns c
  4391.         where
  4392.             c.id = k.id
  4393.             and k.type = 1    /* Primary type key */
  4394.             and c.colid = k.key1
  4395.     if (@@rowcount = 0)
  4396.         goto done
  4397.  
  4398.     insert into #pkeys
  4399.         select
  4400.             db_name(),
  4401.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4402.             object_name(k.id),
  4403.             c.name,
  4404.             2
  4405.         from
  4406.             syskeys k, syscolumns c
  4407.         where
  4408.             c.id = k.id
  4409.             and k.type = 1    /* Primary type key */
  4410.             and c.colid = key2
  4411.     if (@@rowcount = 0)
  4412.         goto done
  4413.  
  4414.     insert into #pkeys
  4415.         select
  4416.             db_name(),
  4417.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4418.             object_name(k.id),
  4419.             c.name,
  4420.             3
  4421.         from
  4422.             syskeys k, syscolumns c
  4423.         where
  4424.             c.id = k.id
  4425.             and k.type = 1    /* Primary type key */
  4426.             and c.colid = key3
  4427.     if (@@rowcount = 0)
  4428.         goto done
  4429.  
  4430.     insert into #pkeys
  4431.         select
  4432.             db_name(),
  4433.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4434.             object_name(k.id),
  4435.             c.name,
  4436.             4
  4437.         from
  4438.             syskeys k, syscolumns c
  4439.         where
  4440.             c.id = k.id
  4441.             and k.type = 1    /* Primary type key */
  4442.             and c.colid = key4
  4443.     if (@@rowcount = 0)
  4444.         goto done
  4445.  
  4446.     insert into #pkeys
  4447.         select
  4448.             db_name(),
  4449.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4450.             object_name(k.id),
  4451.             c.name,
  4452.             5
  4453.         from
  4454.             syskeys k, syscolumns c
  4455.         where
  4456.             c.id = k.id
  4457.             and k.type = 1    /* Primary type key */
  4458.             and c.colid = key5
  4459.     if (@@rowcount = 0)
  4460.         goto done
  4461.  
  4462.     insert into #pkeys
  4463.         select
  4464.             db_name(),
  4465.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4466.             object_name(k.id),
  4467.             c.name,
  4468.             6
  4469.         from
  4470.             syskeys k, syscolumns c
  4471.         where
  4472.             c.id = k.id
  4473.             and k.type = 1    /* Primary type key */
  4474.             and c.colid = key6
  4475.     if (@@rowcount = 0)
  4476.         goto done
  4477.  
  4478.     insert into #pkeys
  4479.         select
  4480.             db_name(),
  4481.             (select user_name(uid) from sysobjects o where o.id = k.id),
  4482.             object_name(k.id),
  4483.             c.name,
  4484.             7
  4485.         from
  4486.             syskeys k, syscolumns c
  4487.         where
  4488.             c.id = k.id
  4489.             and k.type = 1    /* Primary type key */
  4490.             and c.colid = key7
  4491.     if (@@rowcount = 0)
  4492.         goto done
  4493.  
  4494.     insert into #pkeys
  4495.          select
  4496.              db_name(),
  4497.              (select user_name(uid) from sysobjects o where o.id = k.id),
  4498.              object_name(k.id),
  4499.              c.name,
  4500.              8
  4501.          from
  4502.              syskeys k, syscolumns c
  4503.          where
  4504.              c.id = k.id
  4505.              and k.type = 1 /* Primary type key */
  4506.              and c.colid = key8
  4507.  
  4508.     done:
  4509.     select
  4510.         TABLE_QUALIFIER,
  4511.         TABLE_OWNER,
  4512.         TABLE_NAME,
  4513.         COLUMN_NAME,
  4514.         KEY_SEQ,
  4515.         PK_NAME = convert(varchar(32),null)
  4516.     from #pkeys
  4517.     where TABLE_NAME = @table_name
  4518.         and TABLE_OWNER like @table_owner
  4519.     order by 1, 2, 3, 5
  4520. go
  4521.  
  4522. if (charindex('6.00', @@version) = 0 and
  4523.     charindex('6.50', @@version) = 0 and
  4524.     charindex('7.00', @@version) = 0 and
  4525.     charindex('8.00', @@version) = 0)
  4526. begin
  4527.     print ''
  4528.     print ''
  4529.     print 'Warning:'
  4530.     print 'you are installing the stored procedures '
  4531.     print 'on a pre 6.0 SQL Server.'
  4532.     print 'Ignore the following error.'
  4533. end
  4534. else
  4535.     drop proc sp_pkeys
  4536. go
  4537.  
  4538. /*    Procedure for 6.0 and 6.50 servers */
  4539. CREATE PROCEDURE sp_pkeys(
  4540.                @table_name        sysname,
  4541.                @table_owner     sysname = null,
  4542.                @table_qualifier sysname = null )
  4543. as
  4544.     DECLARE @table_id            int
  4545.     DECLARE @full_table_name    varchar(255) /* 2*128 + 1 */
  4546.  
  4547.     if @table_qualifier is not null
  4548.     begin
  4549.         if db_name() <> @table_qualifier
  4550.         begin    /* If qualifier doesn't match current database */
  4551.             raiserror (15250, -1,-1)
  4552.             return
  4553.         end
  4554.     end
  4555.     if @table_owner is null
  4556.     begin    /* If unqualified table name */
  4557.         SELECT @full_table_name = @table_name
  4558.     end
  4559.     else
  4560.     begin    /* Qualified table name */
  4561.         if @table_owner = ''
  4562.         begin    /* If empty owner name */
  4563.             SELECT @full_table_name = @table_owner
  4564.         end
  4565.         else
  4566.         begin
  4567.             SELECT @full_table_name = @table_owner + '.' + @table_name
  4568.         end
  4569.     end
  4570.     /*    Get Object ID */
  4571.     SELECT @table_id = object_id(@full_table_name)
  4572.  
  4573.     select
  4574.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4575.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4576.         TABLE_NAME = convert(sysname,o.name),
  4577.         COLUMN_NAME = convert(sysname,c.name),
  4578.         KEY_SEQ = convert(smallint,c1.colid),
  4579.         PK_NAME = convert(sysname,i.name)
  4580.     from
  4581.         sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  4582.     where
  4583.         o.id = @table_id
  4584.         and o.id = c.id
  4585.         and o.id = i.id
  4586.         and (i.status & 0x800) = 0x800
  4587.         and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4588.         and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4589.         and c1.id = @table_id
  4590.     order by 1, 2, 3, 5
  4591. go
  4592.  
  4593. if (charindex('7.00', @@version) = 0 and
  4594.     charindex('8.00', @@version) = 0)
  4595. begin
  4596.     print ''
  4597.     print ''
  4598.     print 'Warning:'
  4599.     print 'you are installing the stored procedures '
  4600.     print 'on a pre 7.0 SQL Server.'
  4601.     print 'Ignore the following errors.'
  4602. end
  4603. else
  4604.     drop proc sp_pkeys
  4605. go
  4606.  
  4607. /*    Procedure for 8.0 server */
  4608. CREATE PROCEDURE sp_pkeys(
  4609.                @table_name        sysname,
  4610.                @table_owner     sysname = null,
  4611.                @table_qualifier sysname = null )
  4612. as
  4613.     DECLARE @table_id        int
  4614.     DECLARE @full_table_name    nvarchar(255)
  4615.  
  4616.     if @table_qualifier is not null
  4617.     begin
  4618.         if db_name() <> @table_qualifier
  4619.         begin    /* If qualifier doesn't match current database */
  4620.             raiserror (15250, -1,-1)
  4621.             return
  4622.         end
  4623.     end
  4624.     if @table_owner is null
  4625.     begin    /* If unqualified table name */
  4626.         SELECT @full_table_name = quotename(@table_name)
  4627.     end
  4628.     else
  4629.     begin    /* Qualified table name */
  4630.         if @table_owner = ''
  4631.         begin    /* If empty owner name */
  4632.             SELECT @full_table_name = quotename(@table_owner)
  4633.         end
  4634.         else
  4635.         begin
  4636.             SELECT @full_table_name = quotename(@table_owner) +
  4637.                 '.' + quotename(@table_name)
  4638.         end
  4639.     end
  4640.     /*    Get Object ID */
  4641.     SELECT @table_id = object_id(@full_table_name)
  4642.  
  4643.     select
  4644.         TABLE_QUALIFIER = convert(sysname,db_name()),
  4645.         TABLE_OWNER = convert(sysname,user_name(o.uid)),
  4646.         TABLE_NAME = convert(sysname,o.name),
  4647.         COLUMN_NAME = convert(sysname,c.name),
  4648.         --KEY_SEQ = convert(smallint,c.colid),
  4649.         KEY_SEQ =
  4650.             case
  4651.                 when c.name = index_col(@full_table_name, i.indid,  1) then convert (smallint,1)
  4652.                 when c.name = index_col(@full_table_name, i.indid,  2) then convert (smallint,2)
  4653.                 when c.name = index_col(@full_table_name, i.indid,  3) then convert (smallint,3)
  4654.                 when c.name = index_col(@full_table_name, i.indid,  4) then convert (smallint,4)
  4655.                 when c.name = index_col(@full_table_name, i.indid,  5) then convert (smallint,5)
  4656.                 when c.name = index_col(@full_table_name, i.indid,  6) then convert (smallint,6)
  4657.                 when c.name = index_col(@full_table_name, i.indid,  7) then convert (smallint,7)
  4658.                 when c.name = index_col(@full_table_name, i.indid,  8) then convert (smallint,8)
  4659.                 when c.name = index_col(@full_table_name, i.indid,  9) then convert (smallint,9)
  4660.                 when c.name = index_col(@full_table_name, i.indid, 10) then convert (smallint,10)
  4661.                 when c.name = index_col(@full_table_name, i.indid, 11) then convert (smallint,11)
  4662.                 when c.name = index_col(@full_table_name, i.indid, 12) then convert (smallint,12)
  4663.                 when c.name = index_col(@full_table_name, i.indid, 13) then convert (smallint,13)
  4664.                 when c.name = index_col(@full_table_name, i.indid, 14) then convert (smallint,14)
  4665.                 when c.name = index_col(@full_table_name, i.indid, 15) then convert (smallint,15)
  4666.                 when c.name = index_col(@full_table_name, i.indid, 16) then convert (smallint,16)
  4667.             end,
  4668.         PK_NAME = convert(sysname,i.name)
  4669.     from
  4670.         sysindexes i, syscolumns c, sysobjects o --, syscolumns c1
  4671.     where
  4672.         o.id = @table_id
  4673.         and o.id = c.id
  4674.         and o.id = i.id
  4675.         and (i.status & 0x800) = 0x800
  4676.         --and c.name = index_col (@full_table_name, i.indid, c1.colid)
  4677.         and (c.name = index_col (@full_table_name, i.indid,  1) or
  4678.              c.name = index_col (@full_table_name, i.indid,  2) or
  4679.              c.name = index_col (@full_table_name, i.indid,  3) or
  4680.              c.name = index_col (@full_table_name, i.indid,  4) or
  4681.              c.name = index_col (@full_table_name, i.indid,  5) or
  4682.              c.name = index_col (@full_table_name, i.indid,  6) or
  4683.              c.name = index_col (@full_table_name, i.indid,  7) or
  4684.              c.name = index_col (@full_table_name, i.indid,  8) or
  4685.              c.name = index_col (@full_table_name, i.indid,  9) or
  4686.              c.name = index_col (@full_table_name, i.indid, 10) or
  4687.              c.name = index_col (@full_table_name, i.indid, 11) or
  4688.              c.name = index_col (@full_table_name, i.indid, 12) or
  4689.              c.name = index_col (@full_table_name, i.indid, 13) or
  4690.              c.name = index_col (@full_table_name, i.indid, 14) or
  4691.              c.name = index_col (@full_table_name, i.indid, 15) or
  4692.              c.name = index_col (@full_table_name, i.indid, 16)
  4693.             )
  4694.         --and c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  4695.         --and c1.id = @table_id
  4696.     order by 1, 2, 3, 5
  4697. go
  4698.  
  4699.  
  4700. grant execute on sp_pkeys to public
  4701. go
  4702.  
  4703. dump tran master with no_log
  4704. go
  4705.  
  4706. print 'creating sp_server_info'
  4707. go
  4708.  
  4709. create proc sp_server_info (
  4710.             @attribute_id  int = null)
  4711. as
  4712.     if @attribute_id is not null
  4713.         select *
  4714.         from master.dbo.spt_server_info
  4715.         where attribute_id = @attribute_id
  4716.     else
  4717.         select *
  4718.         from master.dbo.spt_server_info
  4719.         order by attribute_id
  4720. go
  4721.  
  4722. grant execute on sp_server_info to public
  4723. go
  4724.  
  4725. dump tran master with no_log
  4726. go
  4727.  
  4728. print 'creating sp_special_columns'
  4729. go
  4730.  
  4731. /*    Procedure for pre-6.0 server */
  4732. CREATE PROCEDURE sp_special_columns (
  4733.                  @table_name        varchar(32),
  4734.                  @table_owner        varchar(32) = null,
  4735.                  @table_qualifier    varchar(32) = null,
  4736.                  @col_type            char(1) = 'R',
  4737.                  @scope                char(1) = 'T',
  4738.                  @nullable            char(1) = 'U',
  4739.                  @ODBCVer            int = 2)
  4740. AS
  4741.     DECLARE @indid                int
  4742.     DECLARE @table_id            int
  4743.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4744.     DECLARE @scopeout            smallint
  4745.  
  4746.     if @col_type not in ('R','V') or @col_type is null
  4747.     begin
  4748.         raiserror 20002 '~~Rush_42~~'
  4749.         return
  4750.     end
  4751.  
  4752.     if @scope = 'C'
  4753.         select @scopeout = 0
  4754.     else if @scope = 'T'
  4755.         select @scopeout = 1
  4756.     else
  4757.     begin
  4758.         raiserror 20002 '~~Rush_43~~'
  4759.         return
  4760.     end
  4761.  
  4762.     if @nullable not in ('U','O') or @nullable is null
  4763.     begin
  4764.         raiserror 20002 '~~Rush_44~~'
  4765.         return
  4766.     end
  4767.  
  4768.     if @table_qualifier is not null
  4769.     begin
  4770.         if db_name() <> @table_qualifier
  4771.         begin /* If qualifier doesn't match current database */
  4772.             raiserror 20001 '~~Rush_5~~'
  4773.             return
  4774.         end
  4775.     end
  4776.     if @table_owner is null
  4777.     begin     /* If unqualified table name */
  4778.         SELECT @full_table_name = @table_name
  4779.     end
  4780.     else
  4781.     begin    /* Qualified table name */
  4782.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4783.     end
  4784.     /*    Get Object ID */
  4785.     SELECT @table_id = object_id(@full_table_name)
  4786.  
  4787.     if @col_type = 'V'
  4788.     BEGIN /* if ROWVER, just run that query */
  4789.         SELECT
  4790.             SCOPE = convert(smallint,NULL),
  4791.             COLUMN_NAME = convert(varchar(32),c.name),
  4792.             DATA_TYPE = convert(smallint, -3),
  4793.             TYPE_NAME = t.name,
  4794.             "PRECISION" = convert(int,8),
  4795.             LENGTH = convert(int,8),
  4796.             SCALE = convert(smallint, NULL),
  4797.             PSEUDO_COLUMN = convert(smallint,1)
  4798.         FROM
  4799.             systypes t, syscolumns c
  4800.         WHERE
  4801.             c.id = @table_id
  4802.             AND c.usertype = 80 /*    TIMESTAMP */
  4803.             AND t.usertype = 80 /*    TIMESTAMP */
  4804.         RETURN
  4805.     END
  4806.  
  4807.     /* ROWID, now find the id of the 'best' index for this table */
  4808.  
  4809.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4810.                            nullable columns. */
  4811.  
  4812.             SELECT @indid = MIN(indid)
  4813.                 FROM sysindexes i,syscolumns c,syscolumns c2
  4814.                 WHERE
  4815.                     i.status&2 = 2        /*    If Unique Index */
  4816.                      AND c.id = i.id
  4817.                      AND c2.id = c.id
  4818.                      AND c2.colid < i.keycnt + (i.status&16)/16
  4819.                     AND i.id = @table_id
  4820.                     AND indid > 0        /*    Eliminate Table Row */
  4821.                     AND c.name = index_col(@table_name,i.indid,c2.colid)
  4822.                     GROUP BY indid HAVING SUM(c.status&8) = 0
  4823.  
  4824.     ELSE    /* Include indexes that are partially nullable. */
  4825.  
  4826.         SELECT @indid = MIN(indid)
  4827.             FROM sysindexes i
  4828.             WHERE
  4829.                 status&2 = 2        /*    If Unique Index */
  4830.                 AND id = @table_id
  4831.                 AND indid > 0        /*    Eliminate Table Row */
  4832.  
  4833.     SELECT
  4834.         SCOPE = @scopeout,
  4835.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4836.         d.DATA_TYPE,
  4837.         TYPE_NAME = t.name,
  4838.         "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  4839.         LENGTH = isnull(d.length, convert(int,c.length)),
  4840.         SCALE = d.numeric_scale,
  4841.         PSEUDO_COLUMN = convert(smallint,1)
  4842.     FROM
  4843.         sysindexes x,
  4844.         syscolumns c,
  4845.         master.dbo.spt_datatype_info d,
  4846.         systypes t,
  4847.         syscolumns c2    /* Self-join to generate list of index columns and */
  4848.                         /* to extract datatype names */
  4849.     WHERE
  4850.         x.id = @table_id
  4851.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  4852.         AND c.id = x.id
  4853.         AND c2.id = x.id
  4854.         AND c2.colid < keycnt+(x.status&16)/16
  4855.         AND x.indid = @indid
  4856.         AND t.type = d.ss_dtype
  4857.         AND c.length = d.fixlen
  4858.         AND c.usertype = t.usertype
  4859.  
  4860. go
  4861.  
  4862. if (charindex('6.00', @@version) = 0 and
  4863.     charindex('6.50', @@version) = 0 and
  4864.     charindex('7.00', @@version) = 0 and
  4865.     charindex('8.00', @@version) = 0)
  4866. begin
  4867.     print ''
  4868.     print ''
  4869.     print 'Warning:'
  4870.     print 'you are installing the stored procedures '
  4871.     print 'on a pre 6.0 SQL Server.'
  4872.     print 'Ignore the following errors.'
  4873. end
  4874. else
  4875.     drop proc sp_special_columns
  4876. go
  4877.  
  4878. /*    Procedure for 6.0 and 6.50 servers */
  4879. CREATE PROCEDURE sp_special_columns (
  4880.                  @table_name        varchar(32),
  4881.                  @table_owner        varchar(32) = null,
  4882.                  @table_qualifier    varchar(32) = null,
  4883.                  @col_type            char(1) = 'R',
  4884.                  @scope                char(1) = 'T',
  4885.                  @nullable            char(1) = 'U',
  4886.                  @ODBCVer            int = 2)
  4887. AS
  4888.     DECLARE @indid                int
  4889.     DECLARE @table_id            int
  4890.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  4891.     DECLARE @scopeout            smallint
  4892.  
  4893.     if @col_type not in ('R','V') or @col_type is null
  4894.     begin
  4895.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  4896.         return
  4897.     end
  4898.  
  4899.     if @scope = 'C'
  4900.         select @scopeout = 0
  4901.     else if @scope = 'T'
  4902.         select @scopeout = 1
  4903.     else
  4904.     begin
  4905.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  4906.         return
  4907.     end
  4908.  
  4909.     if @nullable not in ('U','O') or @nullable is null
  4910.     begin
  4911.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  4912.         return
  4913.     end
  4914.  
  4915.     if @table_qualifier is not null
  4916.     begin
  4917.         if db_name() <> @table_qualifier
  4918.         begin /* If qualifier doesn't match current database */
  4919.             raiserror (15250, -1,-1)
  4920.             return
  4921.         end
  4922.     end
  4923.     if @table_owner is null
  4924.     begin     /* If unqualified table name */
  4925.         SELECT @full_table_name = @table_name
  4926.     end
  4927.     else
  4928.     begin     /* Qualified table name */
  4929.         SELECT @full_table_name = @table_owner + '.' + @table_name
  4930.     end
  4931.     /*    Get Object ID */
  4932.     SELECT @table_id = object_id(@full_table_name)
  4933.  
  4934.     if @col_type = 'V'
  4935.     BEGIN /* if ROWVER, just run that query */
  4936.         SELECT
  4937.             SCOPE = convert(smallint,NULL),
  4938.             COLUMN_NAME = convert(varchar(32),c.name),
  4939.             DATA_TYPE = convert(smallint, -2),
  4940.             TYPE_NAME = t.name,
  4941.             "PRECISION" = convert(int,8),
  4942.             LENGTH = convert(int,8),
  4943.             SCALE = convert(smallint, NULL),
  4944.             PSEUDO_COLUMN = convert(smallint,1)
  4945.         FROM
  4946.             systypes t, syscolumns c
  4947.         WHERE
  4948.             c.id = @table_id
  4949.             AND c.usertype = 80 /*    TIMESTAMP */
  4950.             AND t.usertype = 80 /*    TIMESTAMP */
  4951.         RETURN
  4952.     END
  4953.  
  4954.     /* ROWID, now find the id of the 'best' index for this table */
  4955.  
  4956.     IF @nullable = 'O'    /* Don't include any indexes that contain
  4957.                            nullable columns. */
  4958.  
  4959.         SELECT @indid = MIN(indid)
  4960.             FROM sysindexes i,syscolumns c,syscolumns c2
  4961.             WHERE
  4962.                 i.status&2 = 2        /*    If Unique Index */
  4963.                 AND c.id = i.id
  4964.                 AND c2.id = c.id
  4965.                 AND c2.colid < i.keycnt + (i.status&16)/16
  4966.                 AND i.id = @table_id
  4967.                 AND indid > 0        /*    Eliminate Table Row */
  4968.                 AND c.name = index_col(@table_name,i.indid,c2.colid)
  4969.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  4970.  
  4971.     ELSE    /* Include indexes that are partially nullable. */
  4972.  
  4973.         SELECT @indid = MIN(indid)
  4974.             FROM sysindexes i
  4975.             WHERE
  4976.                 status&2 = 2        /*    If Unique Index */
  4977.                 AND id = @table_id
  4978.                 AND indid > 0        /*    Eliminate Table Row */
  4979.  
  4980.     SELECT
  4981.         SCOPE = @scopeout,
  4982.         COLUMN_NAME = convert(varchar(32),INDEX_COL(@full_table_name,indid,c2.colid)),
  4983.         d.DATA_TYPE,
  4984.         convert(varchar(32),case
  4985.             when (t.usertype > 100 or t.usertype in (18,80))
  4986.                 then t.name
  4987.             else d.TYPE_NAME
  4988.         end) TYPE_NAME,
  4989.         convert(int,case
  4990.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  4991.             else isnull(convert(int,c.prec), 2147483647)
  4992.         end) "PRECISION",
  4993.         convert(int,case
  4994.             when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  4995.                 convert(int,c.prec+2)
  4996.             else
  4997.                 isnull(d.length, c.length)
  4998.         end) LENGTH,
  4999.         SCALE = convert(smallint, c.scale),
  5000.         PSEUDO_COLUMN = convert(smallint,1)
  5001.     FROM
  5002.         sysindexes x,
  5003.         syscolumns c,
  5004.         master.dbo.spt_datatype_info d,
  5005.         systypes t,
  5006.         syscolumns c2    /* Self-join to generate list of index columns and */
  5007.                         /* to extract datatype names */
  5008.     WHERE
  5009.         x.id = @table_id
  5010.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5011.         AND c.id = x.id
  5012.         AND c2.id = x.id
  5013.         AND c2.colid < x.keycnt+(x.status&16)/16
  5014.         AND x.indid = @indid
  5015.         AND t.type = d.ss_dtype
  5016.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5017.         AND isnull(d.AUTO_INCREMENT,0) = (c.status&128)/128
  5018.         AND c.usertype = t.usertype
  5019. go
  5020.  
  5021. if (charindex('7.00', @@version) = 0 and
  5022.     charindex('8.00', @@version) = 0)
  5023. begin
  5024.     print ''
  5025.     print ''
  5026.     print 'Warning:'
  5027.     print 'you are installing the stored procedures '
  5028.     print 'on a pre 7.0 SQL Server.'
  5029.     print 'Ignore the following errors.'
  5030. end
  5031. else
  5032.     drop proc sp_special_columns
  5033. go
  5034.  
  5035. /*    Procedure for 7.0 servers */
  5036. CREATE PROCEDURE sp_special_columns (
  5037.                  @table_name        sysname,
  5038.                  @table_owner        sysname = null,
  5039.                  @table_qualifier    sysname = null,
  5040.                  @col_type            char(1) = 'R',
  5041.                  @scope                char(1) = 'T',
  5042.                  @nullable            char(1) = 'U',
  5043.                  @ODBCVer            int = 2)
  5044. AS
  5045.     DECLARE @indid                int
  5046.     DECLARE @table_id            int
  5047.     DECLARE @full_table_name    nvarchar(257)
  5048.     DECLARE @scopeout            smallint
  5049.  
  5050.     if @col_type not in ('R','V') or @col_type is null
  5051.     begin
  5052.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5053.         return
  5054.     end
  5055.  
  5056.     if @scope = 'C'
  5057.         select @scopeout = 0
  5058.     else if @scope = 'T'
  5059.         select @scopeout = 1
  5060.     else
  5061.     begin
  5062.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5063.         return
  5064.     end
  5065.  
  5066.     if @nullable not in ('U','O') or @nullable is null
  5067.     begin
  5068.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5069.         return
  5070.     end
  5071.  
  5072.     if @table_qualifier is not null
  5073.     begin
  5074.         if db_name() <> @table_qualifier
  5075.         begin /* If qualifier doesn't match current database */
  5076.             raiserror (15250, -1,-1)
  5077.             return
  5078.         end
  5079.     end
  5080.     if @table_owner is null
  5081.     begin     /* If unqualified table name */
  5082.         SELECT @full_table_name = quotename(@table_name)
  5083.     end
  5084.     else
  5085.     begin    /* Qualified table name */
  5086.         if @table_owner = ''
  5087.         begin    /* If empty owner name */
  5088.             SELECT @full_table_name = quotename(@table_owner)
  5089.         end
  5090.         else
  5091.         begin
  5092.             SELECT @full_table_name = quotename(@table_owner) +
  5093.                 '.' + quotename(@table_name)
  5094.         end
  5095.     end
  5096.     /*    Get Object ID */
  5097.     SELECT @table_id = object_id(@full_table_name)
  5098.  
  5099.     if @col_type = 'V'
  5100.     BEGIN /* if ROWVER, just run that query */
  5101.         SELECT
  5102.             SCOPE = convert(smallint,NULL),
  5103.             COLUMN_NAME = convert(sysname,c.name),
  5104.             DATA_TYPE = convert(smallint, -2),
  5105.             TYPE_NAME = t.name,
  5106.             "PRECISION" = convert(int,8),
  5107.             LENGTH = convert(int,8),
  5108.             SCALE = convert(smallint, NULL),
  5109.             PSEUDO_COLUMN = convert(smallint,1)
  5110.         FROM
  5111.             systypes t, syscolumns c
  5112.         WHERE
  5113.             not (@table_id is null)
  5114.             AND c.id = @table_id
  5115.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  5116.             AND t.xtype = c.xtype        
  5117.             AND t.xusertype = c.xusertype    
  5118.         RETURN
  5119.     END
  5120.  
  5121.     /* ROWID, now find the id of the 'best' index for this table */
  5122.  
  5123.     IF @nullable = 'O'    /* Don't include any indexes that contain
  5124.                            nullable columns. */
  5125.  
  5126.         SELECT @indid = MIN(indid)
  5127.             FROM sysindexes x, syscolumns c, syscolumns c2
  5128.             WHERE
  5129.                 not (@table_id is null)
  5130.                 AND x.status&2 = 2        /*    If Unique Index */
  5131.                 AND c.id = x.id
  5132.                 AND c2.id = c.id
  5133.                 AND c2.colid < x.keycnt + (x.status&16)/16
  5134.                 AND x.id = @table_id
  5135.                 AND indid > 0        /*    Eliminate Table Row */
  5136.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5137.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5138.  
  5139.     ELSE    /* Include indexes that are partially nullable. */
  5140.  
  5141.         SELECT @indid = MIN(indid)
  5142.             FROM sysindexes x
  5143.             WHERE
  5144.                 not (@table_id is null)
  5145.                 AND status&2 = 2        /*    If Unique Index */
  5146.                 AND id = @table_id
  5147.                 AND indid > 0        /*    Eliminate Table Row */
  5148.  
  5149.     SELECT
  5150.         SCOPE = @scopeout,
  5151.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5152.         d.DATA_TYPE,
  5153.         convert(sysname,case
  5154.             when t.xusertype > 255 then t.name
  5155.             else d.TYPE_NAME
  5156.         end) TYPE_NAME,
  5157.         convert(int,case
  5158.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5159.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5160.         end) "PRECISION",
  5161.         convert(int,case
  5162.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5163.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5164.             else isnull(d.length, c.length)
  5165.         end) LENGTH,
  5166.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5167.         PSEUDO_COLUMN = convert(smallint,1)
  5168.     FROM
  5169.         sysindexes x,
  5170.         syscolumns c,
  5171.         master.dbo.spt_datatype_info d,
  5172.         systypes t,
  5173.         syscolumns c2    /* Self-join to generate list of index columns and */
  5174.                         /* to extract datatype names */
  5175.     WHERE
  5176.         not (@table_id is null)
  5177.         AND x.id = @table_id
  5178.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5179.         AND c.id = x.id
  5180.         AND c2.id = x.id
  5181.         AND c2.colid < x.keycnt + (x.status&16)/16
  5182.         AND x.indid = @indid
  5183.         AND t.xtype = d.ss_dtype
  5184.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5185.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5186.         AND c.xusertype = t.xusertype
  5187. go
  5188.  
  5189. if (charindex('8.00', @@version) = 0)
  5190. begin
  5191.     print ''
  5192.     print ''
  5193.     print 'Warning:'
  5194.     print 'you are installing the stored procedures '
  5195.     print 'on a pre 8.0 SQL Server.'
  5196.     print 'Ignore the following errors.'
  5197. end
  5198. else
  5199.     drop proc sp_special_columns
  5200. go
  5201.  
  5202. /*    Procedure for 8.0 and later servers */
  5203. CREATE PROCEDURE sp_special_columns (
  5204.                  @table_name        sysname,
  5205.                  @table_owner        sysname = null,
  5206.                  @table_qualifier    sysname = null,
  5207.                  @col_type            char(1) = 'R',
  5208.                  @scope                char(1) = 'T',
  5209.                  @nullable            char(1) = 'U',
  5210.                  @ODBCVer            int = 2)
  5211. AS
  5212.     DECLARE @indid                int
  5213.     DECLARE @table_id            int
  5214.     DECLARE @full_table_name    nvarchar(257)
  5215.     DECLARE @scopeout            smallint
  5216.  
  5217.     if @col_type not in ('R','V') or @col_type is null
  5218.     begin
  5219.         raiserror (15251,-1,-1,'col_type','''R'' or ''V''')
  5220.         return
  5221.     end
  5222.  
  5223.     if @scope = 'C'
  5224.         select @scopeout = 0
  5225.     else if @scope = 'T'
  5226.         select @scopeout = 1
  5227.     else
  5228.     begin
  5229.         raiserror (15251,-1,-1,'scope','''C'' or ''T''')
  5230.         return
  5231.     end
  5232.  
  5233.     if @nullable not in ('U','O') or @nullable is null
  5234.     begin
  5235.         raiserror (15251,-1,-1,'nullable','''U'' or ''O''')
  5236.         return
  5237.     end
  5238.  
  5239.     if @table_qualifier is not null
  5240.     begin
  5241.         if db_name() <> @table_qualifier
  5242.         begin /* If qualifier doesn't match current database */
  5243.             raiserror (15250, -1,-1)
  5244.             return
  5245.         end
  5246.     end
  5247.     if @table_owner is null
  5248.     begin     /* If unqualified table name */
  5249.         SELECT @full_table_name = quotename(@table_name)
  5250.     end
  5251.     else
  5252.     begin    /* Qualified table name */
  5253.         if @table_owner = ''
  5254.         begin    /* If empty owner name */
  5255.             SELECT @full_table_name = quotename(@table_owner)
  5256.         end
  5257.         else
  5258.         begin
  5259.             SELECT @full_table_name = quotename(@table_owner) +
  5260.                 '.' + quotename(@table_name)
  5261.         end
  5262.     end
  5263.     /*    Get Object ID */
  5264.     SELECT @table_id = object_id(@full_table_name)
  5265.  
  5266.     if @col_type = 'V'
  5267.     BEGIN /* if ROWVER, just run that query */
  5268.         SELECT
  5269.             SCOPE = convert(smallint,NULL),
  5270.             COLUMN_NAME = convert(sysname,c.name),
  5271.             DATA_TYPE = convert(smallint, -2),
  5272.             TYPE_NAME = t.name,
  5273.             "PRECISION" = convert(int,8),
  5274.             LENGTH = convert(int,8),
  5275.             SCALE = convert(smallint, NULL),
  5276.             PSEUDO_COLUMN = convert(smallint,1)
  5277.         FROM
  5278.             systypes t, syscolumns c
  5279.         WHERE
  5280.             not (@table_id is null)
  5281.             AND c.id = @table_id
  5282.             AND t.name = 'timestamp'    /*    TIMESTAMP  */
  5283.             AND t.xtype = c.xtype        
  5284.             AND t.xusertype = c.xusertype    
  5285.         RETURN
  5286.     END
  5287.  
  5288.     /* ROWID, now find the id of the 'best' index for this table */
  5289.  
  5290.     IF @nullable = 'O'    /* Don't include any indexes that contain
  5291.                            nullable columns. */
  5292.  
  5293.         SELECT @indid = MIN(indid)
  5294.             FROM sysindexes x, syscolumns c, syscolumns c2
  5295.             WHERE
  5296.                 not (@table_id is null)
  5297.                 AND x.status&2 = 2        /*    If Unique Index */
  5298.                 AND c.id = x.id
  5299.                 AND c2.id = c.id
  5300.                 AND c2.colid < x.keycnt + (x.status&18)/18
  5301.                 AND x.id = @table_id
  5302.                 AND indid > 0        /*    Eliminate Table Row */
  5303.                 AND c.name = index_col(@table_name,x.indid,c2.colid)
  5304.                 GROUP BY indid HAVING SUM(c.status&8) = 0
  5305.  
  5306.     ELSE    /* Include indexes that are partially nullable. */
  5307.  
  5308.         SELECT @indid = MIN(indid)
  5309.             FROM sysindexes x
  5310.             WHERE
  5311.                 not (@table_id is null)
  5312.                 AND status&2 = 2        /*    If Unique Index */
  5313.                 AND id = @table_id
  5314.                 AND indid > 0        /*    Eliminate Table Row */
  5315.  
  5316.     SELECT
  5317.         SCOPE = @scopeout,
  5318.         COLUMN_NAME = convert(sysname,INDEX_COL(@full_table_name,indid,c2.colid)),
  5319.         d.DATA_TYPE,
  5320.         convert(sysname,case
  5321.             when t.xusertype > 255 then t.name
  5322.             else d.TYPE_NAME collate database_default
  5323.         end) TYPE_NAME,
  5324.         convert(int,case
  5325.             when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5326.             else OdbcPrec(c.xtype,c.length,c.xprec)
  5327.         end) "PRECISION",
  5328.         convert(int,case
  5329.             when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5330.                 OdbcPrec(c.xtype,c.length,c.xprec)+2
  5331.             else isnull(d.length, c.length)
  5332.         end) LENGTH,
  5333.         SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5334.         PSEUDO_COLUMN = convert(smallint,1)
  5335.     FROM
  5336.         sysindexes x,
  5337.         syscolumns c,
  5338.         master.dbo.spt_datatype_info d,
  5339.         systypes t,
  5340.         syscolumns c2    /* Self-join to generate list of index columns and */
  5341.                         /* to extract datatype names */
  5342.     WHERE
  5343.         not (@table_id is null)
  5344.         AND x.id = @table_id
  5345.         AND c.name = INDEX_COL(@full_table_name,@indid,c2.colid)
  5346.         AND c.id = x.id
  5347.         AND c2.id = x.id
  5348.         AND c2.colid < x.keycnt + (x.status&16)/16
  5349.         AND x.indid = @indid
  5350.         AND t.xtype = d.ss_dtype
  5351.         AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5352.         AND isnull(d.AUTO_INCREMENT,0) = isnull(ColumnProperty (c.id, c.name, 'IsIdentity'),0)
  5353.         AND c.xusertype = t.xusertype
  5354. go
  5355.  
  5356. grant execute on sp_special_columns to public
  5357. go
  5358.  
  5359. dump tran master with no_log
  5360. go
  5361.  
  5362. print 'creating sp_sproc_columns'
  5363. go
  5364.  
  5365. /*    Procedure for pre-6.0 server */
  5366. CREATE PROCEDURE sp_sproc_columns (
  5367.                  @procedure_name        varchar(102) = '%', /* 3*32+5+1 */
  5368.                  @procedure_owner        varchar(96) = null,
  5369.                  @procedure_qualifier    varchar(32) = null,
  5370.                  @column_name            varchar(96) = null,
  5371.                  @ODBCVer                int = 2)
  5372. AS
  5373.     DECLARE @group_num_lower smallint
  5374.     DECLARE @group_num_upper smallint
  5375.     DECLARE @semi_position int
  5376.     DECLARE @full_procedure_name    varchar(205)/* 2*102+1 */
  5377.     DECLARE @procedure_id int
  5378.  
  5379.     if @column_name is null /*    If column name not supplied, match all */
  5380.         select @column_name = '%'
  5381.     if @procedure_qualifier is not null
  5382.     begin
  5383.         if db_name() <> @procedure_qualifier
  5384.         begin
  5385.             if @procedure_qualifier = ''
  5386.             begin
  5387.                 /* in this case, we need to return an empty result set */
  5388.                 /* because the user has requested a database with an empty name */
  5389.                 select @procedure_name = ''
  5390.                 select @procedure_owner = ''
  5391.             end
  5392.             else
  5393.             begin    /* If qualifier doesn't match current database */
  5394.                 raiserror 20001 '~~Rush_51~~'
  5395.                 return
  5396.             end
  5397.         end
  5398.     end
  5399.  
  5400.     if @procedure_name is null
  5401.     begin    /*    If procedure name not supplied, match all */
  5402.         select @procedure_name = '%'
  5403.     end
  5404.  
  5405.     /* first we need to extract the procedure group number, if one exists */
  5406.     select @semi_position = charindex(';',@procedure_name)
  5407.     if (@semi_position > 0)
  5408.     begin    /* If group number separator (;) found */
  5409.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5410.         select @group_num_upper = @group_num_lower
  5411.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5412.     end
  5413.     else
  5414.     begin    /* No group separator, so default to group number of 1 */
  5415.         select @group_num_lower = 1
  5416.         select @group_num_upper = 32767            
  5417.     end
  5418.  
  5419.     if @procedure_owner is null
  5420.     begin    /* If unqualified procedure name */
  5421.         SELECT @full_procedure_name = @procedure_name
  5422.     end
  5423.     else
  5424.     begin    /* Qualified procedure name */
  5425.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5426.     end
  5427.  
  5428.     /*    Get Object ID */
  5429.     SELECT @procedure_id = object_id(@full_procedure_name)
  5430.     if ((charindex('%',@full_procedure_name) = 0) and
  5431.         (charindex('_',@full_procedure_name) = 0) and
  5432.         @procedure_id <> 0)
  5433.     begin
  5434.         /* this block is for the case where there is no pattern
  5435.             matching required for the procedure name */
  5436.         SELECT
  5437.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5438.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5439.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5440.             COLUMN_NAME = convert(varchar(32),c.name),
  5441.             COLUMN_TYPE = convert(smallint, 0),
  5442.             d.DATA_TYPE,
  5443.             TYPE_NAME = t.name,
  5444.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5445.             LENGTH = isnull(d.length, convert(int,c.length)),
  5446.             SCALE = d.numeric_scale,
  5447.             d.RADIX,
  5448.             d.NULLABLE,
  5449.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5450.             COLUMN_DEF = convert(varchar(255),null),
  5451.             d.SQL_DATA_TYPE,
  5452.             d.SQL_DATETIME_SUB,
  5453.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5454.             ORDINAL_POSITION = convert(int, c.colid),
  5455.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5456.             SS_DATA_TYPE = c.type
  5457.         FROM
  5458.             syscolumns c,
  5459.             sysobjects o,
  5460.             master.dbo.spt_datatype_info d,
  5461.             systypes t
  5462.         WHERE
  5463.             o.id = @procedure_id
  5464.             AND c.id = o.id
  5465.             AND t.type = d.ss_dtype
  5466.             AND c.length = isnull(d.fixlen, c.length)
  5467.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5468.             AND c.usertype = t.usertype
  5469.             AND c.name like @column_name
  5470.             AND c.number between @group_num_lower and @group_num_upper
  5471.         UNION ALL
  5472.         SELECT           /* return value row*/
  5473.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5474.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5475.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5476.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5477.             COLUMN_TYPE = convert(smallint, 5),
  5478.             DATA_TYPE = convert(smallint, 4),
  5479.             TYPE_NAME = convert(varchar(32),'int'),
  5480.             "PRECISION" = convert(int,10),
  5481.             LENGTH = convert(int,4),
  5482.             SCALE = convert(smallint,0),
  5483.             RADIX = convert(smallint,10),
  5484.             NULLABLE = convert(smallint,0),
  5485.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5486.             COLUMN_DEF = convert(varchar(255),NULL),
  5487.             SQL_DATA_TYPE = convert(smallint, 4),
  5488.             SQL_DATETIME_SUB = convert(smallint,null),
  5489.             CHAR_OCTET_LENGTH = convert(int,null),
  5490.             ORDINAL_POSITION = convert(int,0),
  5491.             IS_NULLABLE = convert(varchar(254),'NO'),
  5492.             SS_DATA_TYPE = convert(tinyint,56)
  5493.         FROM
  5494.             syscomments c, sysobjects o
  5495.         WHERE
  5496.             o.id = @procedure_id
  5497.             AND c.id = o.id
  5498.             AND c.colid = 1
  5499.             AND o.type = 'P'                        /* Just Procedures */
  5500.             AND '@RETURN_VALUE' like @column_name
  5501.             AND c.number between @group_num_lower and @group_num_upper        
  5502.         ORDER BY 1, 2, 3, 18
  5503.     end
  5504.     else
  5505.     begin
  5506.         /* this block is for the case where there IS pattern
  5507.             matching done on the procedure name */
  5508.         if @procedure_owner is null
  5509.             select @procedure_owner = '%'
  5510.         SELECT
  5511.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5512.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5513.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5514.             COLUMN_NAME = convert(varchar(32),c.name),
  5515.             COLUMN_TYPE = convert(smallint, 0),
  5516.             d.DATA_TYPE,
  5517.             TYPE_NAME = t.name,
  5518.             "PRECISION" = isnull(d.data_precision, convert(int,c.length)),
  5519.             LENGTH = isnull(d.length, convert(int,c.length)),
  5520.             SCALE = d.numeric_scale,
  5521.             d.RADIX,
  5522.             d.NULLABLE,
  5523.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5524.             COLUMN_DEF = convert(varchar(255),null),
  5525.             d.SQL_DATA_TYPE,
  5526.             d.SQL_DATETIME_SUB,
  5527.             CHAR_OCTET_LENGTH = isnull(d.data_precision, convert(int,c.length))+d.charbin,
  5528.             ORDINAL_POSITION = convert(int, c.colid),
  5529.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5530.             SS_DATA_TYPE = c.type
  5531.         FROM
  5532.             syscolumns c,
  5533.             sysobjects o,
  5534.             master.dbo.spt_datatype_info d,
  5535.             systypes t
  5536.         WHERE
  5537.             o.name like @procedure_name
  5538.             AND user_name(o.uid) like @procedure_owner
  5539.             AND o.id = c.id
  5540.             AND t.type = d.ss_dtype
  5541.             AND c.length = isnull(d.fixlen, c.length)
  5542.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5543.             AND c.usertype = t.usertype
  5544.             AND o.type = 'P'                            /* Just Procedures */
  5545.             AND c.name like @column_name
  5546.             AND c.number between @group_num_lower and @group_num_upper
  5547.         UNION ALL
  5548.         SELECT           /* return value row*/
  5549.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5550.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5551.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5552.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5553.             COLUMN_TYPE = convert(smallint, 5),
  5554.             DATA_TYPE = convert(smallint, 4),
  5555.             TYPE_NAME = convert(varchar(32),'int'),
  5556.             "PRECISION" = convert(int,10),
  5557.             LENGTH = convert(int,4),
  5558.             SCALE = convert(smallint,0),
  5559.             RADIX = convert(smallint,10),
  5560.             NULLABLE = convert(smallint,0),
  5561.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5562.             COLUMN_DEF = convert(varchar(255),NULL),
  5563.             SQL_DATA_TYPE = convert(smallint, 4),
  5564.             SQL_DATETIME_SUB = convert(smallint,null),
  5565.             CHAR_OCTET_LENGTH = convert(int,null),
  5566.             ORDINAL_POSITION = convert(int,0),
  5567.             IS_NULLABLE = convert(varchar(254),'NO'),
  5568.             SS_DATA_TYPE = convert(tinyint,56)
  5569.         FROM
  5570.             syscomments c, sysobjects o
  5571.         WHERE
  5572.             o.name like @procedure_name
  5573.             AND user_name(o.uid) like @procedure_owner
  5574.             AND c.id = o.id
  5575.             AND c.colid = 1
  5576.             AND o.type = 'P'                        /* Just Procedures */
  5577.             AND '@RETURN_VALUE' like @column_name
  5578.             AND c.number between @group_num_lower and @group_num_upper
  5579.         ORDER BY 1, 2, 3, 18
  5580.     end
  5581. go
  5582.  
  5583. if (charindex('6.00', @@version) = 0 and
  5584.     charindex('6.50', @@version) = 0 and
  5585.     charindex('7.00', @@version) = 0 and
  5586.     charindex('8.00', @@version) = 0)
  5587. begin
  5588.     print ''
  5589.     print ''
  5590.     print 'Warning:'
  5591.     print 'you are installing the stored procedures '
  5592.     print 'on a pre 6.0 SQL Server.'
  5593.     print 'Ignore the following error.'
  5594. end
  5595. else
  5596.     drop proc sp_sproc_columns
  5597. go
  5598.  
  5599. /*    Procedure for 6.0 and 6.50 servers */
  5600. CREATE PROCEDURE sp_sproc_columns (
  5601.                  @procedure_name        varchar(102) = '%', /* 3*36+5+1 */
  5602.                  @procedure_owner        varchar(96) = null,
  5603.                  @procedure_qualifier    varchar(32) = null,
  5604.                  @column_name            varchar(96) = null,
  5605.                  @ODBCVer                int = 2)
  5606. AS
  5607.     DECLARE @group_num_lower smallint
  5608.     DECLARE @group_num_upper smallint
  5609.     DECLARE @semi_position int
  5610.     DECLARE @full_procedure_name    varchar(205)
  5611.     DECLARE @procedure_id int
  5612.  
  5613.     if @column_name is null /*    If column name not supplied, match all */
  5614.         select @column_name = '%'
  5615.     if @procedure_qualifier is not null
  5616.     begin
  5617.         if db_name() <> @procedure_qualifier
  5618.         begin
  5619.             if @procedure_qualifier = ''
  5620.             begin
  5621.                 /* in this case, we need to return an empty result set */
  5622.                 /* because the user has requested a database with an empty name */
  5623.                 select @procedure_name = ''
  5624.                 select @procedure_owner = ''
  5625.             end
  5626.             else
  5627.             begin    /* If qualifier doesn't match current database */
  5628.                 raiserror (15250, -1,-1)
  5629.                 return
  5630.             end
  5631.         end
  5632.     end
  5633.  
  5634.     if @procedure_name is null
  5635.     begin    /*    If procedure name not supplied, match all */
  5636.         select @procedure_name = '%'
  5637.     end
  5638.  
  5639.     /* first we need to extract the procedure group number, if one exists */
  5640.     select @semi_position = charindex(';',@procedure_name)
  5641.     if (@semi_position > 0)
  5642.     begin    /* If group number separator (;) found */
  5643.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5644.         select @group_num_upper = @group_num_lower
  5645.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5646.     end
  5647.     else
  5648.     begin    /* No group separator, so default to group number of 1 */
  5649.         select @group_num_lower = 1
  5650.         select @group_num_upper = 32767            
  5651.     end
  5652.  
  5653.     if @procedure_owner is null
  5654.     begin    /* If unqualified procedure name */
  5655.         SELECT @full_procedure_name = @procedure_name
  5656.     end
  5657.     else
  5658.     begin    /* Qualified procedure name */
  5659.         SELECT @full_procedure_name = @procedure_owner + '.' + @procedure_name
  5660.     end
  5661.  
  5662.     /*    Get Object ID */
  5663.     SELECT @procedure_id = object_id(@full_procedure_name)
  5664.     if ((charindex('%',@full_procedure_name) = 0) and
  5665.         (charindex('[',@full_procedure_name) = 0) and
  5666.         (charindex('_',@full_procedure_name) = 0) and
  5667.         @procedure_id <> 0)
  5668.     begin
  5669.         /* this block is for the case where there is no pattern
  5670.             matching required for the procedure name */
  5671.         SELECT
  5672.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5673.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5674.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5675.             COLUMN_NAME = convert(varchar(32),c.name),
  5676.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5677.             d.DATA_TYPE,
  5678.             TYPE_NAME = t.name,
  5679.             convert(int,case
  5680.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5681.                 else isnull(convert(int,c.prec), 2147483647)
  5682.             end) "PRECISION",
  5683.             convert(int,case
  5684.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5685.                     c.prec+2
  5686.                 else
  5687.                     isnull(d.length, c.length)
  5688.             end) LENGTH,
  5689.             SCALE = convert(smallint, c.scale),
  5690.             d.RADIX,
  5691.             d.NULLABLE,
  5692.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5693.             COLUMN_DEF = convert(varchar(255),NULL),
  5694.             d.SQL_DATA_TYPE,
  5695.             d.SQL_DATETIME_SUB,
  5696.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5697.             ORDINAL_POSITION = convert(int, c.colid),
  5698.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5699.             SS_DATA_TYPE = c.type
  5700.         FROM
  5701.             syscolumns c,
  5702.             sysobjects o,
  5703.             master.dbo.spt_datatype_info d,
  5704.             systypes t
  5705.         WHERE
  5706.             o.id = @procedure_id
  5707.             AND c.id = o.id
  5708.             AND c.type = d.ss_dtype
  5709.             AND c.length = isnull(d.fixlen, c.length)
  5710.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5711.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5712.             AND c.usertype = t.usertype
  5713.             AND c.name like @column_name
  5714.             AND c.number between @group_num_lower and @group_num_upper
  5715.         UNION ALL
  5716.         SELECT           /* return value row*/
  5717.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5718.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5719.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5720.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5721.             COLUMN_TYPE = convert(smallint, 5),
  5722.             DATA_TYPE = convert(smallint, 4),
  5723.             TYPE_NAME = convert(varchar(32),'int'),
  5724.             "PRECISION" = convert(int,10),
  5725.             LENGTH = convert(int,4),
  5726.             SCALE = convert(smallint,0),
  5727.             RADIX = convert(smallint,10),
  5728.             NULLABLE = convert(smallint,0),
  5729.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5730.             COLUMN_DEF = convert(varchar(255),NULL),
  5731.             SQL_DATA_TYPE = convert(smallint, 4),
  5732.             SQL_DATETIME_SUB = convert(smallint,null),
  5733.             CHAR_OCTET_LENGTH = convert(int,null),
  5734.             ORDINAL_POSITION = convert(int,0),
  5735.             IS_NULLABLE = convert(varchar(254),'NO'),
  5736.             SS_DATA_TYPE = convert(tinyint,56)
  5737.         FROM
  5738.             syscomments c, sysobjects o
  5739.         WHERE
  5740.             o.id = @procedure_id
  5741.             AND c.id = o.id
  5742.             AND c.colid = 1
  5743.             AND o.type = 'P'                        /* Just Procedures */
  5744.             AND '@RETURN_VALUE' like @column_name
  5745.             AND c.number between @group_num_lower and @group_num_upper
  5746.         ORDER BY 1, 2, 3, 18
  5747.     end
  5748.     else
  5749.     begin
  5750.         /* this block is for the case where there IS pattern
  5751.             matching done on the procedure name */
  5752.         if @procedure_owner is null
  5753.             select @procedure_owner = '%'
  5754.         SELECT
  5755.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5756.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5757.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  5758.             COLUMN_NAME = convert(varchar(32),c.name),
  5759.             COLUMN_TYPE = convert(smallint, 1+((c.status/64)&1)),
  5760.             d.DATA_TYPE,
  5761.             TYPE_NAME = t.name,
  5762.             convert(int,case
  5763.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5764.                 else isnull(convert(int,c.prec), 2147483647)
  5765.             end) "PRECISION",
  5766.             convert(int,case
  5767.                 when d.ss_dtype IN (106, 108, 55, 63) then    /* decimal/numeric types */
  5768.                     c.prec+2
  5769.                 else
  5770.                     isnull(d.length, c.length)
  5771.             end) LENGTH,
  5772.             SCALE = convert(smallint, c.scale),
  5773.             d.RADIX,
  5774.             d.NULLABLE,
  5775.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5776.             COLUMN_DEF = convert(varchar(255),NULL),
  5777.             d.SQL_DATA_TYPE,
  5778.             d.SQL_DATETIME_SUB,
  5779.             CHAR_OCTET_LENGTH = isnull(convert(int,c.prec), 2147483647)+d.charbin,
  5780.             ORDINAL_POSITION = convert(int, c.colid),
  5781.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5782.             SS_DATA_TYPE = c.type
  5783.         FROM
  5784.             syscolumns c,
  5785.             sysobjects o,
  5786.             master.dbo.spt_datatype_info d,
  5787.             systypes t
  5788.         WHERE
  5789.             o.name like @procedure_name
  5790.             AND user_name(o.uid) like @procedure_owner
  5791.             AND o.id = c.id
  5792.             AND c.type = d.ss_dtype
  5793.             AND c.length = isnull(d.fixlen, c.length)
  5794.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5795.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5796.             AND c.usertype = t.usertype
  5797.             AND o.type = 'P'                            /* Just Procedures */
  5798.             AND c.name like @column_name
  5799.             AND c.number between @group_num_lower and @group_num_upper
  5800.         UNION ALL
  5801.         SELECT           /* return value row*/
  5802.             PROCEDURE_QUALIFIER = convert(varchar(32),DB_NAME()),
  5803.             PROCEDURE_OWNER = convert(varchar(32),USER_NAME(o.uid)),
  5804.             PROCEDURE_NAME = convert(varchar(36),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  5805.             COLUMN_NAME = convert(varchar(32),'@RETURN_VALUE'),
  5806.             COLUMN_TYPE = convert(smallint, 5),
  5807.             DATA_TYPE = convert(smallint, 4),
  5808.             TYPE_NAME = convert(varchar(32),'int'),
  5809.             "PRECISION" = convert(int,10),
  5810.             LENGTH = convert(int,4),
  5811.             SCALE = convert(smallint,0),
  5812.             RADIX = convert(smallint,10),
  5813.             NULLABLE = convert(smallint,0),
  5814.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5815.             COLUMN_DEF = convert(varchar(255),NULL),
  5816.             SQL_DATA_TYPE = convert(smallint, 4),
  5817.             SQL_DATETIME_SUB = convert(smallint,null),
  5818.             CHAR_OCTET_LENGTH = convert(int,null),
  5819.             ORDINAL_POSITION = convert(int,0),
  5820.             IS_NULLABLE = convert(varchar(254),'NO'),
  5821.             SS_DATA_TYPE = convert(tinyint,56)
  5822.         FROM
  5823.             syscomments c, sysobjects o
  5824.         WHERE
  5825.             o.name like @procedure_name
  5826.             AND user_name(o.uid) like @procedure_owner
  5827.             AND c.id = o.id
  5828.             AND c.colid = 1
  5829.             AND o.type = 'P'                        /* Just Procedures */
  5830.             AND '@RETURN_VALUE' like @column_name
  5831.             AND c.number between @group_num_lower and @group_num_upper
  5832.         ORDER BY 1, 2, 3, 18
  5833.     end
  5834. go
  5835.  
  5836. if (charindex('7.00', @@version) = 0 and
  5837.     charindex('8.00', @@version) = 0)
  5838. begin
  5839.     print ''
  5840.     print ''
  5841.     print 'Warning:'
  5842.     print 'you are installing the stored procedures '
  5843.     print 'on a pre 7.0 SQL Server.'
  5844.     print 'Ignore the following errors.'
  5845. end
  5846. else
  5847.     drop proc sp_sproc_columns
  5848. go
  5849.  
  5850. /*    Procedure for 7.0 server */
  5851. CREATE PROCEDURE sp_sproc_columns (
  5852.                  @procedure_name        nvarchar(390) = '%',
  5853.                  @procedure_owner        nvarchar(384) = null,
  5854.                  @procedure_qualifier    sysname = null,
  5855.                  @column_name            nvarchar(384) = null,
  5856.                  @ODBCVer                int = 2)
  5857. AS
  5858.     DECLARE @group_num_lower smallint
  5859.     DECLARE @group_num_upper smallint
  5860.     DECLARE @semi_position int
  5861.     DECLARE @full_procedure_name    nvarchar(774)
  5862.     DECLARE @procedure_id int
  5863.  
  5864.     if @column_name is null /*    If column name not supplied, match all */
  5865.         select @column_name = '%'
  5866.     if @procedure_qualifier is not null
  5867.     begin
  5868.         if db_name() <> @procedure_qualifier
  5869.         begin
  5870.             if @procedure_qualifier = ''
  5871.             begin
  5872.                 /* in this case, we need to return an empty result set */
  5873.                 /* because the user has requested a database with an empty name */
  5874.                 select @procedure_name = ''
  5875.                 select @procedure_owner = ''
  5876.             end
  5877.             else
  5878.             begin    /* If qualifier doesn't match current database */
  5879.                 raiserror (15250, -1,-1)
  5880.                 return
  5881.             end
  5882.         end
  5883.     end
  5884.  
  5885.     if @procedure_name is null
  5886.     begin    /*    If procedure name not supplied, match all */
  5887.         select @procedure_name = '%'
  5888.     end
  5889.  
  5890.     /* first we need to extract the procedure group number, if one exists */
  5891.     select @semi_position = charindex(';',@procedure_name)
  5892.     if (@semi_position > 0)
  5893.     begin    /* If group number separator (;) found */
  5894.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  5895.         select @group_num_upper = @group_num_lower
  5896.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  5897.     end
  5898.     else
  5899.     begin    /* No group separator, so default to all groups */
  5900.         select @group_num_lower = 1
  5901.         select @group_num_upper = 32767            
  5902.     end
  5903.  
  5904.     if @procedure_owner is null
  5905.     begin    /* If unqualified procedure name */
  5906.         SELECT @full_procedure_name = quotename(@procedure_name)
  5907.     end
  5908.     else
  5909.     begin    /* Qualified procedure name */
  5910.         if @procedure_owner = ''
  5911.         begin    /* If empty owner name */
  5912.             SELECT @full_procedure_name = quotename(@procedure_owner)
  5913.         end
  5914.         else
  5915.         begin
  5916.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  5917.                 '.' + quotename(@procedure_name)
  5918.         end
  5919.     end
  5920.  
  5921.     /*    Get Object ID */
  5922.     SELECT @procedure_id = object_id(@full_procedure_name)
  5923.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  5924.         (isnull(charindex('[', @procedure_name),0) = 0) and
  5925.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  5926.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  5927.         not (@procedure_id is null))
  5928.     begin
  5929.         /* this block is for the case where there is no pattern
  5930.             matching required for the procedure name */
  5931.         SELECT
  5932.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5933.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5934.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5935.             COLUMN_NAME = convert(sysname,c.name),
  5936.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  5937.             d.DATA_TYPE,
  5938.             TYPE_NAME = t.name,
  5939.             convert(int,case
  5940.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  5941.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  5942.             end) "PRECISION",
  5943.             convert(int,case
  5944.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  5945.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  5946.                 else
  5947.                     isnull(d.length, c.length)
  5948.             end) LENGTH,
  5949.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  5950.             d.RADIX,
  5951.             d.NULLABLE,
  5952.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5953.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5954.             d.SQL_DATA_TYPE,
  5955.             d.SQL_DATETIME_SUB,
  5956.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  5957.             ORDINAL_POSITION = convert(int, c.colid),
  5958.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  5959.             SS_DATA_TYPE = c.type
  5960.         FROM
  5961.             syscolumns c,
  5962.             sysobjects o,
  5963.             master.dbo.spt_datatype_info d,
  5964.             systypes t
  5965.         WHERE
  5966.             o.id = @procedure_id
  5967.             AND c.id = o.id
  5968.             AND c.xtype = d.ss_dtype
  5969.             AND c.length = isnull(d.fixlen, c.length)
  5970.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  5971.             AND isnull(d.AUTO_INCREMENT,0) = 0
  5972.             AND c.xusertype = t.xusertype
  5973.             AND c.name like @column_name
  5974.             AND c.number between @group_num_lower and @group_num_upper
  5975.         UNION ALL
  5976.         SELECT           /* return value row*/
  5977.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  5978.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  5979.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  5980.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  5981.             COLUMN_TYPE = convert(smallint, 5),
  5982.             DATA_TYPE = convert(smallint, 4),
  5983.             TYPE_NAME = convert(sysname,'int'),
  5984.             "PRECISION" = convert(int,10),
  5985.             LENGTH = convert(int,4),
  5986.             SCALE = convert(smallint,0),
  5987.             RADIX = convert(smallint,10),
  5988.             NULLABLE = convert(smallint,0),
  5989.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  5990.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  5991.             SQL_DATA_TYPE = convert(smallint, 4),
  5992.             SQL_DATETIME_SUB = convert(smallint,null),
  5993.             CHAR_OCTET_LENGTH = convert(int,null),
  5994.             ORDINAL_POSITION = convert(int,0),
  5995.             IS_NULLABLE = convert(varchar(254),'NO'),
  5996.             SS_DATA_TYPE = convert(tinyint,56)
  5997.         FROM
  5998.             syscomments c, sysobjects o
  5999.         WHERE
  6000.             o.id = @procedure_id
  6001.             AND c.id = o.id
  6002.             AND c.colid = 1
  6003.             AND o.type = 'P'                        /* Just Procedures */
  6004.             AND '@RETURN_VALUE' like @column_name
  6005.             AND c.number between @group_num_lower and @group_num_upper        
  6006.         ORDER BY 1, 2, 3, 18
  6007.     end
  6008.     else
  6009.     begin
  6010.         /* this block is for the case where there IS pattern
  6011.             matching done on the procedure name */
  6012.         if @procedure_owner is null
  6013.             select @procedure_owner = '%'
  6014.         SELECT
  6015.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6016.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6017.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6018.             COLUMN_NAME = convert(sysname,c.name),
  6019.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  6020.             d.DATA_TYPE,
  6021.             TYPE_NAME = t.name,
  6022.             convert(int,case
  6023.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6024.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6025.             end) "PRECISION",
  6026.             convert(int,case
  6027.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6028.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6029.                 else
  6030.                     isnull(d.length, c.length)
  6031.             end) LENGTH,
  6032.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6033.             d.RADIX,
  6034.             d.NULLABLE,
  6035.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6036.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6037.             d.SQL_DATA_TYPE,
  6038.             d.SQL_DATETIME_SUB,
  6039.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6040.             ORDINAL_POSITION = convert(int, c.colid),
  6041.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6042.             SS_DATA_TYPE = c.type
  6043.         FROM
  6044.             syscolumns c,
  6045.             sysobjects o,
  6046.             master.dbo.spt_datatype_info d,
  6047.             systypes t
  6048.         WHERE
  6049.             o.name like @procedure_name
  6050.             AND user_name(o.uid) like @procedure_owner
  6051.             AND o.id = c.id
  6052.             AND c.xtype = d.ss_dtype
  6053.             AND c.length = isnull(d.fixlen, c.length)
  6054.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6055.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6056.             AND c.xusertype = t.xusertype
  6057.             AND o.type = 'P'                            /* Just Procedures */
  6058.             AND c.name like @column_name
  6059.             AND c.number between @group_num_lower and @group_num_upper
  6060.         UNION ALL
  6061.         SELECT           /* return value row*/
  6062.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6063.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6064.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6065.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6066.             COLUMN_TYPE = convert(smallint, 5),
  6067.             DATA_TYPE = convert(smallint, 4),
  6068.             TYPE_NAME = convert(sysname,'int'),
  6069.             "PRECISION" = convert(int,10),
  6070.             LENGTH = convert(int,4),
  6071.             SCALE = convert(smallint,0),
  6072.             RADIX = convert(smallint,10),
  6073.             NULLABLE = convert(smallint,0),
  6074.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6075.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6076.             SQL_DATA_TYPE = convert(smallint, 4),
  6077.             SQL_DATETIME_SUB = convert(smallint,null),
  6078.             CHAR_OCTET_LENGTH = convert(int,null),
  6079.             ORDINAL_POSITION = convert(int,0),
  6080.             IS_NULLABLE = convert(varchar(254),'NO'),
  6081.             SS_DATA_TYPE = convert(tinyint,56)
  6082.         FROM
  6083.             syscomments c, sysobjects o
  6084.         WHERE
  6085.             o.name like @procedure_name
  6086.             AND c.id = o.id
  6087.             AND user_name(o.uid) like @procedure_owner
  6088.             AND c.colid = 1
  6089.             AND o.type = 'P'                        /* Just Procedures */
  6090.             AND '@RETURN_VALUE' like @column_name
  6091.             AND c.number between @group_num_lower and @group_num_upper
  6092.         ORDER BY 1, 2, 3, 18
  6093.     end
  6094. go
  6095.  
  6096. if (charindex('8.00', @@version) = 0)
  6097. begin
  6098.     print ''
  6099.     print ''
  6100.     print 'Warning:'
  6101.     print 'you are installing the stored procedures '
  6102.     print 'on a pre 8.0 SQL Server.'
  6103.     print 'Ignore the following errors.'
  6104. end
  6105. else
  6106.     drop proc sp_sproc_columns
  6107. go
  6108.  
  6109. /*    Procedure for 8.0 server */
  6110. CREATE PROCEDURE sp_sproc_columns (
  6111.                  @procedure_name        nvarchar(390) = '%',
  6112.                  @procedure_owner        nvarchar(384) = null,
  6113.                  @procedure_qualifier        sysname = null,
  6114.                  @column_name            nvarchar(384) = null,
  6115.                  @ODBCVer                int = 2)
  6116. AS
  6117.     DECLARE @group_num_lower smallint
  6118.     DECLARE @group_num_upper smallint
  6119.     DECLARE @semi_position int
  6120.     DECLARE @full_procedure_name nvarchar(774)
  6121.     DECLARE @procedure_id int
  6122.  
  6123.     if @column_name is null /*    If column name not supplied, match all */
  6124.         select @column_name = '%'
  6125.     if @procedure_qualifier is not null
  6126.     begin
  6127.         if db_name() <> @procedure_qualifier
  6128.         begin
  6129.             if @procedure_qualifier = ''
  6130.             begin
  6131.                 /* in this case, we need to return an empty result set */
  6132.                 /* because the user has requested a database with an empty name */
  6133.                 select @procedure_name = ''
  6134.                 select @procedure_owner = ''
  6135.             end
  6136.             else
  6137.             begin    /* If qualifier doesn't match current database */
  6138.                 raiserror (15250, -1,-1)
  6139.                 return
  6140.             end
  6141.         end
  6142.     end
  6143.  
  6144.     if @procedure_name is null
  6145.     begin    /*    If procedure name not supplied, match all */
  6146.         select @procedure_name = '%'
  6147.     end
  6148.  
  6149.     /* first we need to extract the procedure group number, if one exists */
  6150.     select @semi_position = charindex(';',@procedure_name)
  6151.     if (@semi_position > 0)
  6152.     begin    /* If group number separator (;) found */
  6153.         select @group_num_lower = convert(int,substring(@procedure_name, @semi_position + 1, 2))
  6154.         select @group_num_upper = @group_num_lower
  6155.         select @procedure_name = substring(@procedure_name, 1, @semi_position -1)
  6156.     end
  6157.     else
  6158.     begin    /* No group separator, so default to all groups */
  6159.         select @group_num_lower = 1
  6160.         select @group_num_upper = 32767            
  6161.     end
  6162.  
  6163.     if @procedure_owner is null
  6164.     begin    /* If unqualified procedure name */
  6165.         SELECT @full_procedure_name = quotename(@procedure_name)
  6166.     end
  6167.     else
  6168.     begin    /* Qualified procedure name */
  6169.         if @procedure_owner = ''
  6170.         begin    /* If empty owner name */
  6171.             SELECT @full_procedure_name = quotename(@procedure_owner)
  6172.         end
  6173.         else
  6174.         begin
  6175.             SELECT @full_procedure_name = quotename(@procedure_owner) +
  6176.                 '.' + quotename(@procedure_name)
  6177.         end
  6178.     end
  6179.  
  6180.     /*    Get Object ID */
  6181.     SELECT @procedure_id = object_id(@full_procedure_name)
  6182.     if ((isnull(charindex('%', @full_procedure_name),0) = 0) and
  6183.         (isnull(charindex('[', @procedure_name),0) = 0) and
  6184.         (isnull(charindex('[', @procedure_owner),0) = 0) and
  6185.         (isnull(charindex('_', @full_procedure_name),0) = 0) and
  6186.         not (@procedure_id is null))
  6187.     begin
  6188.         /* this block is for the case where there is no pattern
  6189.             matching required for the procedure name */
  6190.         SELECT
  6191.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6192.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6193.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6194.             COLUMN_NAME = convert(sysname,c.name),
  6195.             COLUMN_TYPE = convert(smallint, 1+c.isoutparam),
  6196.             d.DATA_TYPE,
  6197.             TYPE_NAME = t.name,
  6198.             convert(int,case
  6199.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6200.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6201.             end) "PRECISION",
  6202.             convert(int,case
  6203.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6204.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6205.                 else
  6206.                     isnull(d.length, c.length)
  6207.             end) LENGTH,
  6208.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6209.             d.RADIX,
  6210.             d.NULLABLE,
  6211.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6212.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6213.             d.SQL_DATA_TYPE,
  6214.             d.SQL_DATETIME_SUB,
  6215.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6216.             ORDINAL_POSITION = convert(int, c.colid),
  6217.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6218.             SS_DATA_TYPE = c.type
  6219.         FROM
  6220.             syscolumns c,
  6221.             sysobjects o,
  6222.             master.dbo.spt_datatype_info d,
  6223.             systypes t
  6224.         WHERE
  6225.             o.id = @procedure_id
  6226.             AND c.id = o.id
  6227.             AND c.xtype = d.ss_dtype
  6228.             AND c.length = isnull(d.fixlen, c.length)
  6229.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6230.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6231.             AND c.xusertype = t.xusertype
  6232.             AND c.name like @column_name
  6233.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6234.             AND ((c.number between @group_num_lower and @group_num_upper)
  6235.                  OR (c.number = 0 and o.type = 'FN'))
  6236.         UNION ALL
  6237.         SELECT           /* return value row*/
  6238.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6239.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6240.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6241.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6242.             COLUMN_TYPE = convert(smallint, 5),
  6243.             DATA_TYPE = convert(smallint, 4),
  6244.             TYPE_NAME = convert(sysname,'int'),
  6245.             "PRECISION" = convert(int,10),
  6246.             LENGTH = convert(int,4),
  6247.             SCALE = convert(smallint,0),
  6248.             RADIX = convert(smallint,10),
  6249.             NULLABLE = convert(smallint,0),
  6250.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6251.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6252.             SQL_DATA_TYPE = convert(smallint, 4),
  6253.             SQL_DATETIME_SUB = convert(smallint,null),
  6254.             CHAR_OCTET_LENGTH = convert(int,null),
  6255.             ORDINAL_POSITION = convert(int,0),
  6256.             IS_NULLABLE = convert(varchar(254),'NO'),
  6257.             SS_DATA_TYPE = convert(tinyint,56)
  6258.         FROM
  6259.             syscomments c, sysobjects o
  6260.         WHERE
  6261.             o.id = @procedure_id
  6262.             AND c.id = o.id
  6263.             AND c.colid = 1
  6264.             AND o.type = 'P'            /* Procedures */
  6265.             AND '@RETURN_VALUE' like @column_name
  6266.             AND c.number between @group_num_lower and @group_num_upper
  6267.         UNION ALL
  6268.         SELECT        /* UDF return value */
  6269.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6270.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6271.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6272.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6273.             COLUMN_TYPE = convert(smallint, 5),    /* SQL_RETURN_VALUE */
  6274.             d.DATA_TYPE,
  6275.             TYPE_NAME = t.name,
  6276.             convert(int,case
  6277.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6278.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6279.             end) "PRECISION",
  6280.             convert(int,case
  6281.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6282.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6283.                 else
  6284.                     isnull(d.length, c.length)
  6285.             end) LENGTH,
  6286.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6287.             d.RADIX,
  6288.             NULLABLE = convert(smallint, c.isnullable),
  6289.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6290.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6291.             d.SQL_DATA_TYPE,
  6292.             d.SQL_DATETIME_SUB,
  6293.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6294.             ORDINAL_POSITION = convert(int,0),
  6295.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6296.             SS_DATA_TYPE = c.type
  6297.         FROM
  6298.             syscolumns c,
  6299.             sysobjects o,
  6300.             master.dbo.spt_datatype_info d,
  6301.             systypes t
  6302.         WHERE
  6303.             o.name like @procedure_name
  6304.             AND o.id = c.id
  6305.             AND c.xtype = d.ss_dtype
  6306.             AND c.length = isnull(d.fixlen, c.length)
  6307.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6308.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6309.             AND c.xusertype = t.xusertype
  6310.             AND o.type = 'FN'            /* Scalar UDF */
  6311.             AND c.name like @column_name
  6312.             AND c.colid = 0
  6313.             AND c.number = 0
  6314.         UNION ALL
  6315.         SELECT        /* Table valued functions */
  6316.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6317.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6318.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6319.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6320.             COLUMN_TYPE = convert(smallint, 3),
  6321.             DATA_TYPE = convert(smallint, NULL),
  6322.             TYPE_NAME = convert(sysname, 'table'),
  6323.             "PRECISION" = convert(int,0),
  6324.             LENGTH = convert(int,0),
  6325.             SCALE = convert(smallint,0),
  6326.             RADIX = convert(smallint,0),
  6327.             NULLABLE = convert(smallint,0),
  6328.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6329.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6330.             SQL_DATA_TYPE = convert(smallint, NULL),
  6331.             SQL_DATETIME_SUB = convert(smallint,null),
  6332.             CHAR_OCTET_LENGTH = convert(int,null),
  6333.             ORDINAL_POSITION = convert(int,0),
  6334.             IS_NULLABLE = convert(varchar(254),'NO'),
  6335.             SS_DATA_TYPE = convert(tinyint,0)
  6336.         FROM
  6337.             syscomments c, sysobjects o
  6338.         WHERE
  6339.             o.id = @procedure_id
  6340.             AND c.id = o.id
  6341.             AND c.colid = 1
  6342.             AND o.type IN ('TF', 'IF')
  6343.             AND '@TABLE_RETURN_VALUE' like @column_name
  6344.             AND c.number = 0
  6345.         ORDER BY 1, 2, 3, 18
  6346.     end
  6347.     else
  6348.     begin
  6349.         /* this block is for the case where there IS pattern
  6350.             matching done on the procedure name */
  6351.         if @procedure_owner is null
  6352.             select @procedure_owner = '%'
  6353.  
  6354.         SELECT
  6355.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6356.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6357.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6358.             COLUMN_NAME = convert(sysname,c.name),
  6359.             COLUMN_TYPE = convert(smallint,    1+c.isoutparam),
  6360.             d.DATA_TYPE,
  6361.             TYPE_NAME = t.name,
  6362.             convert(int,case
  6363.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6364.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6365.             end) "PRECISION",
  6366.             convert(int,case
  6367.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6368.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6369.                 else
  6370.                     isnull(d.length, c.length)
  6371.             end) LENGTH,
  6372.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6373.             d.RADIX,
  6374.             d.NULLABLE,
  6375.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6376.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6377.             d.SQL_DATA_TYPE,
  6378.             d.SQL_DATETIME_SUB,
  6379.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6380.             ORDINAL_POSITION = convert(int, c.colid),
  6381.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6382.             SS_DATA_TYPE = c.type
  6383.         FROM
  6384.             syscolumns c,
  6385.             sysobjects o,
  6386.             master.dbo.spt_datatype_info d,
  6387.             systypes t
  6388.         WHERE
  6389.             o.name like @procedure_name
  6390.             AND user_name(o.uid) like @procedure_owner
  6391.             AND o.id = c.id
  6392.             AND c.xtype = d.ss_dtype
  6393.             AND c.length = isnull(d.fixlen, c.length)
  6394.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6395.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6396.             AND c.xusertype = t.xusertype
  6397.             AND c.name like @column_name
  6398.             AND (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  6399.             AND ((c.number between @group_num_lower and @group_num_upper)
  6400.                  OR (c.number = 0 and o.type = 'FN'))
  6401.         UNION ALL
  6402.         SELECT           /* return value row*/
  6403.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6404.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6405.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ isnull(ltrim(str(c.number,5)),'1')),
  6406.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6407.             COLUMN_TYPE = convert(smallint, 5),    /* SQL_RETURN_VALUE */
  6408.             DATA_TYPE = convert(smallint, 4),
  6409.             TYPE_NAME = convert(sysname,'int'),
  6410.             "PRECISION" = convert(int,10),
  6411.             LENGTH = convert(int,4),
  6412.             SCALE = convert(smallint,0),
  6413.             RADIX = convert(smallint,10),
  6414.             NULLABLE = convert(smallint,0),
  6415.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6416.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6417.             SQL_DATA_TYPE = convert(smallint, 4),
  6418.             SQL_DATETIME_SUB = convert(smallint,null),
  6419.             CHAR_OCTET_LENGTH = convert(int,null),
  6420.             ORDINAL_POSITION = convert(int,0),
  6421.             IS_NULLABLE = convert(varchar(254),'NO'),
  6422.             SS_DATA_TYPE = convert(tinyint,56)
  6423.         FROM
  6424.             syscomments c, sysobjects o
  6425.         WHERE
  6426.             o.name like @procedure_name
  6427.             AND c.id = o.id
  6428.             AND user_name(o.uid) like @procedure_owner
  6429.             AND c.colid = 1
  6430.             AND o.type = 'P'                    /* Procedures */
  6431.             AND '@RETURN_VALUE' like @column_name
  6432.             AND c.number between @group_num_lower and @group_num_upper
  6433.         UNION ALL
  6434.         SELECT        /* UDF return value */
  6435.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6436.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6437.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  6438.             COLUMN_NAME = convert(sysname,'@RETURN_VALUE'),
  6439.             COLUMN_TYPE = convert(smallint, 5),    /* SQL_RETURN_VALUE */
  6440.             d.DATA_TYPE,
  6441.             TYPE_NAME = t.name,
  6442.             convert(int,case
  6443.                 when d.DATA_TYPE in (6,7) then d.data_precision         /* FLOAT/REAL */
  6444.                 else OdbcPrec(c.xtype,c.length,c.xprec)
  6445.             end) "PRECISION",
  6446.             convert(int,case
  6447.                 when type_name(d.ss_dtype) IN ('numeric','decimal') then    /* decimal/numeric types */
  6448.                     OdbcPrec(c.xtype,c.length,c.xprec)+2
  6449.                 else
  6450.                     isnull(d.length, c.length)
  6451.             end) LENGTH,
  6452.             SCALE = convert(smallint, OdbcScale(c.xtype,c.xscale)),
  6453.             d.RADIX,
  6454.             NULLABLE = convert(smallint, c.isnullable),
  6455.             REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  6456.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6457.             d.SQL_DATA_TYPE,
  6458.             d.SQL_DATETIME_SUB,
  6459.             CHAR_OCTET_LENGTH = isnull(d.length, c.length)+d.charbin,
  6460.             ORDINAL_POSITION = convert(int,0),
  6461.             IS_NULLABLE = convert(varchar(254),rtrim(substring('NO YES',d.NULLABLE*3+1,3))),
  6462.             SS_DATA_TYPE = c.type
  6463.         FROM
  6464.             syscolumns c,
  6465.             sysobjects o,
  6466.             master.dbo.spt_datatype_info d,
  6467.             systypes t
  6468.         WHERE
  6469.             o.name like @procedure_name
  6470.             AND user_name(o.uid) like @procedure_owner
  6471.             AND o.id = c.id
  6472.             AND c.xtype = d.ss_dtype
  6473.             AND c.length = isnull(d.fixlen, c.length)
  6474.             AND (d.ODBCVer is null or d.ODBCVer = @ODBCVer)
  6475.             AND isnull(d.AUTO_INCREMENT,0) = 0
  6476.             AND c.xusertype = t.xusertype
  6477.             AND o.type = 'FN'            /* Scalar UDF */
  6478.             AND c.name like @column_name
  6479.             AND c.colid = 0
  6480.             AND c.number = 0
  6481.         UNION ALL
  6482.         SELECT        /* Table valued functions */
  6483.             PROCEDURE_QUALIFIER = convert(sysname,DB_NAME()),
  6484.             PROCEDURE_OWNER = convert(sysname,USER_NAME(o.uid)),
  6485.             PROCEDURE_NAME = convert(nvarchar(134),o.name +';1'),
  6486.             COLUMN_NAME = convert(sysname,'@TABLE_RETURN_VALUE'),
  6487.             COLUMN_TYPE = convert(smallint, 3),
  6488.             DATA_TYPE = convert(smallint, NULL),
  6489.             TYPE_NAME = convert(sysname, 'table'),
  6490.             "PRECISION" = convert(int,0),
  6491.             LENGTH = convert(int,0),
  6492.             SCALE = convert(smallint,0),
  6493.             RADIX = convert(smallint,0),
  6494.             NULLABLE = convert(smallint,0),
  6495.             REMARKS = convert(varchar(254), 'Result table returned by table valued function'),
  6496.             COLUMN_DEF = convert(nvarchar(4000),NULL),
  6497.             SQL_DATA_TYPE = convert(smallint, NULL),
  6498.             SQL_DATETIME_SUB = convert(smallint,null),
  6499.             CHAR_OCTET_LENGTH = convert(int,null),
  6500.             ORDINAL_POSITION = convert(int,0),
  6501.             IS_NULLABLE = convert(varchar(254),'NO'),
  6502.             SS_DATA_TYPE = convert(tinyint,0)
  6503.         FROM
  6504.             syscomments c, sysobjects o
  6505.         WHERE
  6506.             o.name like @procedure_name
  6507.             AND user_name(o.uid) like @procedure_owner
  6508.             AND c.id = o.id
  6509.             AND c.colid = 1
  6510.             AND o.type IN ('TF', 'IF')
  6511.             AND '@TABLE_RETURN_VALUE' like @column_name
  6512.             AND c.number = 0
  6513.         ORDER BY 1, 2, 3, 18
  6514.     end
  6515. go
  6516.  
  6517. grant execute on sp_sproc_columns to public
  6518. go
  6519.  
  6520. dump tran master with no_log
  6521. go
  6522.  
  6523. print 'creating sp_statistics'
  6524. go
  6525.  
  6526. /*    Procedure for pre-7.0 server */
  6527. CREATE PROCEDURE sp_statistics (
  6528.                  @table_name        varchar(32),
  6529.                  @table_owner        varchar(32) = null,
  6530.                  @table_qualifier    varchar(32) = null,
  6531.                  @index_name        varchar(32) = '%',
  6532.                  @is_unique         char(1) = 'N',
  6533.                  @accuracy            char(1) = 'Q')
  6534. AS
  6535.     set nocount on
  6536.     DECLARE @indid                int
  6537.     DECLARE @lastindid            int
  6538.     DECLARE @table_id            int
  6539.     DECLARE @full_table_name    varchar(65) /* 2*32+1 */
  6540.  
  6541.     create table #TmpIndex(
  6542.         TABLE_QUALIFIER varchar(32) NULL,
  6543.         TABLE_OWNER     varchar(32) NULL,
  6544.         TABLE_NAME        varchar(32) NOT NULL,
  6545.         INDEX_QUALIFIER varchar(32) null,
  6546.         INDEX_NAME        varchar(32) null,
  6547.         NON_UNIQUE        smallint null,
  6548.         TYPE            smallint NOT NULL,
  6549.         SEQ_IN_INDEX    smallint null,
  6550.         COLUMN_NAME     varchar(32) null,
  6551.         COLLATION        char(1) null,
  6552.         index_id        int null,
  6553.         CARDINALITY     int null,
  6554.         PAGES            int null,
  6555.         status            smallint NOT NULL)
  6556.  
  6557.     if @table_qualifier is not null
  6558.     begin
  6559.         if db_name() <> @table_qualifier
  6560.         begin    /* If qualifier doesn't match current database */
  6561.             raiserror 20001 '~~Rush_5~~'
  6562.             return
  6563.         end
  6564.     end
  6565.  
  6566.     if @accuracy not in ('Q','E')
  6567.         begin
  6568.             raiserror 20002 '~~Rush_58~~'
  6569.             return
  6570.         end
  6571.  
  6572.     if (@@trancount <> 0 and
  6573.         charindex('6.50', @@version) = 0 and
  6574.         charindex('7.00', @@version) = 0 and
  6575.         charindex('8.00', @@version) = 0)
  6576.     begin    /* If inside a transaction */
  6577.         raiserror 20003 '~~Rush_59~~'
  6578.         return
  6579.     end
  6580.  
  6581.     if @table_owner is null
  6582.     begin    /* If unqualified table name */
  6583.         SELECT @full_table_name = @table_name
  6584.     end
  6585.     else
  6586.     begin    /* Qualified table name */
  6587.         if @table_owner = ''
  6588.         begin    /* If empty owner name */
  6589.             SELECT @full_table_name = @table_owner
  6590.         end
  6591.         else
  6592.         begin
  6593.             SELECT @full_table_name = @table_owner + '.' + @table_name
  6594.         end
  6595.     end
  6596.     /*    Get Object ID */
  6597.     SELECT @table_id = object_id(@full_table_name)
  6598.  
  6599.     /*    Start at lowest index id */
  6600.     SELECT @indid = min(indid)
  6601.     FROM sysindexes
  6602.     WHERE id = @table_id
  6603.         AND indid > 0
  6604.         AND indid < 255
  6605.  
  6606.     WHILE @indid is not NULL
  6607.     BEGIN
  6608.         INSERT #TmpIndex    /* Add all columns that are in index */
  6609.             SELECT
  6610.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  6611.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  6612.                 o.name,                                 /* TABLE_NAME       */
  6613.                 o.name,                                 /* INDEX_QUALIFIER */
  6614.                 x.name,                                 /* INDEX_NAME       */
  6615.                 0,                                        /* NON_UNIQUE       */
  6616.                 1,                                        /* SQL_INDEX_CLUSTERED */
  6617.                 colid,                                    /* SEQ_IN_INDEX    */
  6618.                 INDEX_COL(@full_table_name,indid,colid),/* COLUMN_NAME       */
  6619.                 'A',                                    /* COLLATION       */
  6620.                 @indid,                                 /* index_id        */
  6621.                 x.rows,                                 /* CARDINALITY       */
  6622.                 x.dpages,                                /* PAGES           */
  6623.                 x.status                                /* status            */
  6624.             FROM sysindexes x, syscolumns c, sysobjects o
  6625.             WHERE
  6626.                 x.id = @table_id
  6627.                 AND x.id = o.id
  6628.                 AND x.id = c.id
  6629.                 AND c.colid < keycnt+(x.status&16)/16    /* all but Unique Clust indices have an extra key */
  6630.                 AND x.indid = @indid
  6631.         /*
  6632.         **      Now move @indid to the next index.
  6633.         */
  6634.         SELECT @lastindid = @indid
  6635.         SELECT @indid = NULL
  6636.  
  6637.         SELECT @indid = min(indid)
  6638.         FROM sysindexes
  6639.         WHERE id = @table_id
  6640.             AND indid > @lastindid
  6641.             AND indid < 255
  6642.     END
  6643.  
  6644.     UPDATE #TmpIndex
  6645.         SET NON_UNIQUE = 1
  6646.         WHERE status&2 <> 2 /* If non-unique index */
  6647.     UPDATE #TmpIndex
  6648.         SET
  6649.             TYPE = 3,            /* SQL_INDEX_OTHER */
  6650.             CARDINALITY = NULL,
  6651.             PAGES = NULL
  6652.         WHERE index_id > 1    /* If non-clustered index */
  6653.  
  6654.     /* now add row for table statistics */
  6655.     INSERT #TmpIndex
  6656.         SELECT
  6657.             DB_NAME(),                /* TABLE_QUALIFIER */
  6658.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  6659.             o.name,                 /* TABLE_NAME       */
  6660.             null,                    /* INDEX_QUALIFIER */
  6661.             null,                    /* INDEX_NAME       */
  6662.             null,                    /* NON_UNIQUE       */
  6663.             0,                        /* SQL_TABLE_STAT  */
  6664.             null,                    /* SEQ_IN_INDEX    */
  6665.             null,                    /* COLUMN_NAME       */
  6666.             null,                    /* COLLATION       */
  6667.             0,                        /* index_id        */
  6668.             x.rows,                 /* CARDINALITY       */
  6669.             x.dpages,                /* PAGES           */
  6670.             0                        /* status           */
  6671.         FROM sysindexes x, sysobjects o
  6672.         WHERE o.id = @table_id
  6673.             AND x.id = o.id
  6674.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  6675.                                                 /*    then table stats are in */
  6676.                                                 /*    a row with indid =0        */
  6677.  
  6678.     if @is_unique <> 'Y'    /* If all indexes desired */
  6679.         SELECT
  6680.             TABLE_QUALIFIER,
  6681.             TABLE_OWNER,
  6682.             TABLE_NAME,
  6683.             NON_UNIQUE,
  6684.             INDEX_QUALIFIER,
  6685.             INDEX_NAME,
  6686.             TYPE,
  6687.             SEQ_IN_INDEX,
  6688.             COLUMN_NAME,
  6689.             COLLATION,
  6690.             CARDINALITY,
  6691.             PAGES,
  6692.             FILTER_CONDITION = convert(varchar(128),null)
  6693.         FROM #TmpIndex
  6694.         WHERE
  6695.             INDEX_NAME like @index_name /* If matching name */
  6696.             or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  6697.         ORDER BY 4, 7, 6, 8
  6698.     else                    /* If only unique indexes desired */
  6699.         SELECT
  6700.             TABLE_QUALIFIER,
  6701.             TABLE_OWNER,
  6702.             TABLE_NAME,
  6703.             NON_UNIQUE,
  6704.             INDEX_QUALIFIER,
  6705.             INDEX_NAME,
  6706.             TYPE,
  6707.             SEQ_IN_INDEX,
  6708.             COLUMN_NAME,
  6709.             COLLATION,
  6710.             CARDINALITY,
  6711.             PAGES,
  6712.             FILTER_CONDITION = convert(varchar(128),null)
  6713.         FROM #TmpIndex
  6714.         WHERE
  6715.             (NON_UNIQUE = 0             /* If unique */
  6716.                 or NON_UNIQUE is NULL)    /* If SQL_TABLE_STAT row */
  6717.             and (INDEX_NAME like @index_name    /* If matching name */
  6718.                 or INDEX_NAME is null)    /* If SQL_TABLE_STAT row */
  6719.         ORDER BY 4, 7, 6, 8
  6720.  
  6721.     DROP TABLE #TmpIndex
  6722. go
  6723.  
  6724. if (charindex('7.00', @@version) = 0 and
  6725.     charindex('8.00', @@version) = 0)
  6726. begin
  6727.     print ''
  6728.     print ''
  6729.     print 'Warning:'
  6730.     print 'you are installing the stored procedures '
  6731.     print 'on a pre 7.0 SQL Server.'
  6732.     print 'Ignore the following errors.'
  6733. end
  6734. else
  6735.     drop proc sp_statistics
  6736. go
  6737.  
  6738. /*    Procedure for 7.0 server */
  6739. CREATE PROCEDURE sp_statistics (
  6740.                  @table_name        sysname,
  6741.                  @table_owner        sysname = null,
  6742.                  @table_qualifier    sysname = null,
  6743.                  @index_name        sysname = '%',
  6744.                  @is_unique         char(1) = 'N',
  6745.                  @accuracy            char(1) = 'Q')
  6746. AS
  6747.     set nocount on
  6748.     DECLARE @indid                int
  6749.     DECLARE @lastindid            int
  6750.     DECLARE @table_id            int
  6751.     DECLARE @full_table_name    nvarchar(257)
  6752.  
  6753.     create table #TmpIndex(
  6754.         TABLE_QUALIFIER sysname NULL,
  6755.         TABLE_OWNER     sysname NULL,
  6756.         TABLE_NAME        sysname NOT NULL,
  6757.         INDEX_QUALIFIER sysname null,
  6758.         INDEX_NAME        sysname null,
  6759.         NON_UNIQUE        smallint null,
  6760.         TYPE            smallint NOT NULL,
  6761.         SEQ_IN_INDEX    smallint null,
  6762.         COLUMN_NAME     sysname null,
  6763.         COLLATION        char(1) null,
  6764.         index_id        int null,
  6765.         CARDINALITY     int null,
  6766.         PAGES            int null,
  6767.         status            int NOT NULL)
  6768.  
  6769.     if @table_qualifier is not null
  6770.     begin
  6771.         if db_name() <> @table_qualifier
  6772.         begin    /* If qualifier doesn't match current database */
  6773.             raiserror (15250, -1,-1)
  6774.             return
  6775.         end
  6776.     end
  6777.  
  6778.     if @accuracy not in ('Q','E')
  6779.         begin
  6780.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  6781.             return
  6782.         end
  6783.  
  6784.     if @table_owner is null
  6785.     begin    /* If unqualified table name */
  6786.         SELECT @full_table_name = quotename(@table_name)
  6787.     end
  6788.     else
  6789.     begin    /* Qualified table name */
  6790.         if @table_owner = ''
  6791.         begin    /* If empty owner name */
  6792.             SELECT @full_table_name = quotename(@table_owner)
  6793.         end
  6794.         else
  6795.         begin
  6796.             SELECT @full_table_name = quotename(@table_owner) +
  6797.                 '.' + quotename(@table_name)
  6798.         end
  6799.     end
  6800.     /*    Get Object ID */
  6801.     SELECT @table_id = object_id(@full_table_name)
  6802.  
  6803.     /*    Start at lowest index id */
  6804.     SELECT @indid = min(indid)
  6805.     FROM sysindexes
  6806.     WHERE not (@table_id is null)
  6807.         AND id = @table_id
  6808.         AND indid > 0
  6809.         AND indid < 255
  6810.  
  6811.     /* Create a temp table to correct the ordinal position of the columns */
  6812.     create table #TmpColumns
  6813.     (ordinal int identity(1,1),
  6814.      colid   smallint not null)
  6815.  
  6816.     /* Load columns into the temp table */
  6817.     insert into #TmpColumns (colid)
  6818.     select c.colid
  6819.     from syscolumns c
  6820.     where c.id = @table_id
  6821.     order by c.colid
  6822.     
  6823.     WHILE @indid is not NULL
  6824.     BEGIN
  6825.         INSERT #TmpIndex    /* Add all columns that are in index */
  6826.             SELECT
  6827.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  6828.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  6829.                 o.name,                                 /* TABLE_NAME       */
  6830.                 o.name,                                 /* INDEX_QUALIFIER */
  6831.                 x.name,                                 /* INDEX_NAME       */
  6832.                 case                                    /* NON_UNIQUE       */
  6833.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  6834.                     else 0                                /* Unique index    */
  6835.                 end,
  6836.                 case                                    /* TYPE            */
  6837.                     when @indid > 1 then 3                /* Non-Clustered   */
  6838.                     else 1                                /* Clustered index */
  6839.                 end,
  6840.                 tc.ordinal,                                    /* SEQ_IN_INDEX    */
  6841.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME       */
  6842.                 'A',                                    /* COLLATION       */
  6843.                 @indid,                                 /* index_id        */
  6844.                 case                                    /* CARDINALITY       */
  6845.                     when @indid > 1 then NULL            /* Non-Clustered   */
  6846.                     else x.rows                         /* Clustered index */
  6847.                 end,
  6848.                 case                                    /* PAGES           */
  6849.                     when @indid > 1 then NULL            /* Non-Clustered   */
  6850.                     else x.dpages                        /* Clustered index */
  6851.                 end,
  6852.                 x.status                                /* status            */
  6853.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  6854.             WHERE
  6855.                 not (@table_id is null)
  6856.                 AND x.id = @table_id
  6857.                 AND x.id = o.id
  6858.                 AND x.id = c.id
  6859.                 AND tc.colid = c.colid
  6860.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  6861.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  6862.                 AND indid = @indid
  6863.                 AND (x.status&2 = 2
  6864.                     OR @is_unique <> 'Y')
  6865.                 AND (x.status&32) = 0
  6866.         /*
  6867.         **      Now move @indid to the next index.
  6868.         */
  6869.         SELECT @lastindid = @indid
  6870.         SELECT @indid = NULL
  6871.  
  6872.         SELECT @indid = min(indid)
  6873.         FROM sysindexes
  6874.         WHERE not (@table_id is null)
  6875.             AND id = @table_id
  6876.             AND indid > @lastindid
  6877.             AND indid < 255
  6878.     END
  6879.  
  6880.     /* now add row for table statistics */
  6881.     INSERT #TmpIndex
  6882.         SELECT
  6883.             DB_NAME(),                /* TABLE_QUALIFIER */
  6884.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  6885.             o.name,                 /* TABLE_NAME       */
  6886.             null,                    /* INDEX_QUALIFIER */
  6887.             null,                    /* INDEX_NAME       */
  6888.             null,                    /* NON_UNIQUE       */
  6889.             0,                        /* SQL_TABLE_STAT  */
  6890.             null,                    /* SEQ_IN_INDEX    */
  6891.             null,                    /* COLUMN_NAME       */
  6892.             null,                    /* COLLATION       */
  6893.             0,                        /* index_id        */
  6894.             x.rows,                 /* CARDINALITY       */
  6895.             x.dpages,                /* PAGES           */
  6896.             0                        /* status           */
  6897.         FROM sysindexes x, sysobjects o
  6898.         WHERE not (@table_id is null)
  6899.             AND o.id = @table_id
  6900.             AND x.id = o.id
  6901.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  6902.                                                 /*    then table stats are in */
  6903.                                                 /*    a row with indid =0        */
  6904.  
  6905.     SELECT
  6906.         TABLE_QUALIFIER,
  6907.         TABLE_OWNER,
  6908.         TABLE_NAME,
  6909.         NON_UNIQUE,
  6910.         INDEX_QUALIFIER,
  6911.         INDEX_NAME,
  6912.         TYPE,
  6913.         SEQ_IN_INDEX,
  6914.         COLUMN_NAME,
  6915.         COLLATION,
  6916.         CARDINALITY,
  6917.         PAGES,
  6918.         FILTER_CONDITION = convert(varchar(128),null)
  6919.     FROM #TmpIndex
  6920.     WHERE
  6921.         INDEX_NAME like @index_name /* If matching name */
  6922.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  6923.     ORDER BY 4, 7, 6, 8
  6924.  
  6925.     DROP TABLE #TmpIndex, #TmpColumns
  6926. go
  6927.  
  6928. if (charindex('8.00', @@version) = 0)
  6929. begin
  6930.     print ''
  6931.     print ''
  6932.     print 'Warning:'
  6933.     print 'you are installing the stored procedures '
  6934.     print 'on a pre 8.0 SQL Server.'
  6935.     print 'Ignore the following errors.'
  6936. end
  6937. else
  6938.     drop proc sp_statistics
  6939. go
  6940.  
  6941. /*    Procedure for 8.0 server */
  6942. CREATE PROCEDURE sp_statistics (
  6943.                  @table_name        sysname,
  6944.                  @table_owner        sysname = null,
  6945.                  @table_qualifier    sysname = null,
  6946.                  @index_name        sysname = '%',
  6947.                  @is_unique         char(1) = 'N',
  6948.                  @accuracy            char(1) = 'Q')
  6949. AS
  6950.     set nocount on
  6951.     DECLARE @indid                int
  6952.     DECLARE @lastindid            int
  6953.     DECLARE @table_id            int
  6954.     DECLARE @full_table_name    nvarchar(257)
  6955.  
  6956.     create table #TmpIndex(
  6957.         TABLE_QUALIFIER sysname collate database_default NULL,
  6958.         TABLE_OWNER     sysname collate database_default NULL,
  6959.         TABLE_NAME        sysname collate database_default NOT NULL,
  6960.         INDEX_QUALIFIER sysname collate database_default null,
  6961.         INDEX_NAME        sysname collate database_default null,
  6962.         NON_UNIQUE        smallint null,
  6963.         TYPE            smallint NOT NULL,
  6964.         SEQ_IN_INDEX    smallint null,
  6965.         COLUMN_NAME     sysname collate database_default null,
  6966.         COLLATION        char(1) collate database_default null,
  6967.         index_id        int null,
  6968.         CARDINALITY     int null,
  6969.         PAGES            int null,
  6970.         status            int NOT NULL)
  6971.  
  6972.     if @table_qualifier is not null
  6973.     begin
  6974.         if db_name() <> @table_qualifier
  6975.         begin    /* If qualifier doesn't match current database */
  6976.             raiserror (15250, -1,-1)
  6977.             return
  6978.         end
  6979.     end
  6980.  
  6981.     if @accuracy not in ('Q','E')
  6982.         begin
  6983.             raiserror (15251,-1,-1,'accuracy','''Q'' or ''E''')
  6984.             return
  6985.         end
  6986.  
  6987.     if @table_owner is null
  6988.     begin    /* If unqualified table name */
  6989.         SELECT @full_table_name = quotename(@table_name)
  6990.     end
  6991.     else
  6992.     begin    /* Qualified table name */
  6993.         if @table_owner = ''
  6994.         begin    /* If empty owner name */
  6995.             SELECT @full_table_name = quotename(@table_owner)
  6996.         end
  6997.         else
  6998.         begin
  6999.             SELECT @full_table_name = quotename(@table_owner) +
  7000.                 '.' + quotename(@table_name)
  7001.         end
  7002.     end
  7003.     /*    Get Object ID */
  7004.     SELECT @table_id = object_id(@full_table_name)
  7005.  
  7006.     /*    Start at lowest index id */
  7007.     SELECT @indid = min(indid)
  7008.     FROM sysindexes
  7009.     WHERE not (@table_id is null)
  7010.         AND id = @table_id
  7011.         AND indid > 0
  7012.         AND indid < 255
  7013.  
  7014.     /* Create a temp table to correct the ordinal position of the columns */
  7015.     create table #TmpColumns
  7016.     (ordinal int identity(1,1),
  7017.      colid   smallint not null)
  7018.  
  7019.     /* Load columns into the temp table */
  7020.     insert into #TmpColumns (colid)
  7021.     select c.colid
  7022.     from syscolumns c
  7023.     where c.id = @table_id
  7024.     order by c.colid
  7025.     
  7026.     WHILE @indid is not NULL
  7027.     BEGIN
  7028.         INSERT #TmpIndex    /* Add all columns that are in index */
  7029.             SELECT
  7030.                 DB_NAME(),                                /* TABLE_QUALIFIER */
  7031.                 USER_NAME(o.uid),                        /* TABLE_OWNER       */
  7032.                 o.name,                                 /* TABLE_NAME       */
  7033.                 o.name,                                 /* INDEX_QUALIFIER */
  7034.                 x.name,                                 /* INDEX_NAME       */
  7035.                 case                                    /* NON_UNIQUE       */
  7036.                     WHEN x.status&2 <> 2 then 1            /* Nonunique index */
  7037.                     else 0                                /* Unique index    */
  7038.                 end,
  7039.                 case                                    /* TYPE            */
  7040.                     when @indid > 1 then 3                /* Non-Clustered   */
  7041.                     else 1                                /* Clustered index */
  7042.                 end,
  7043.                 tc.ordinal,                                    /* SEQ_IN_INDEX    */
  7044.                 INDEX_COL(@full_table_name, indid, tc.ordinal),/* COLUMN_NAME       */
  7045.                 'A',                                    /* COLLATION       */
  7046.                 @indid,                                 /* index_id        */
  7047.                 case                                    /* CARDINALITY       */
  7048.                     when @indid > 1 then NULL            /* Non-Clustered   */
  7049.                     else x.rows                         /* Clustered index */
  7050.                 end,
  7051.                 case                                    /* PAGES           */
  7052.                     when @indid > 1 then NULL            /* Non-Clustered   */
  7053.                     else x.dpages                        /* Clustered index */
  7054.                 end,
  7055.                 x.status                                /* status            */
  7056.             FROM sysindexes x, syscolumns c, sysobjects o, #TmpColumns tc
  7057.             WHERE
  7058.                 not (@table_id is null)
  7059.                 AND x.id = @table_id
  7060.                 AND x.id = o.id
  7061.                 AND x.id = c.id
  7062.                 AND tc.colid = c.colid
  7063.                 AND tc.ordinal < keycnt+(x.status&18)/18    /* all but Unique Clust indices have an extra key */
  7064.                 AND INDEX_COL(@full_table_name, indid, tc.ordinal) IS NOT NULL
  7065.                 AND indid = @indid
  7066.                 AND (x.status&2 = 2
  7067.                     OR @is_unique <> 'Y')
  7068.                 AND (x.status&32) = 0
  7069.         /*
  7070.         **      Now move @indid to the next index.
  7071.         */
  7072.         SELECT @lastindid = @indid
  7073.         SELECT @indid = NULL
  7074.  
  7075.         SELECT @indid = min(indid)
  7076.         FROM sysindexes
  7077.         WHERE not (@table_id is null)
  7078.             AND id = @table_id
  7079.             AND indid > @lastindid
  7080.             AND indid < 255
  7081.     END
  7082.  
  7083.     /* now add row for table statistics */
  7084.     INSERT #TmpIndex
  7085.         SELECT
  7086.             DB_NAME(),                /* TABLE_QUALIFIER */
  7087.             USER_NAME(o.uid),        /* TABLE_OWNER       */
  7088.             o.name,                 /* TABLE_NAME       */
  7089.             null,                    /* INDEX_QUALIFIER */
  7090.             null,                    /* INDEX_NAME       */
  7091.             null,                    /* NON_UNIQUE       */
  7092.             0,                        /* SQL_TABLE_STAT  */
  7093.             null,                    /* SEQ_IN_INDEX    */
  7094.             null,                    /* COLUMN_NAME       */
  7095.             null,                    /* COLLATION       */
  7096.             0,                        /* index_id        */
  7097.             x.rows,                 /* CARDINALITY       */
  7098.             x.dpages,                /* PAGES           */
  7099.             0                        /* status           */
  7100.         FROM sysindexes x, sysobjects o
  7101.         WHERE not (@table_id is null)
  7102.             AND o.id = @table_id
  7103.             AND x.id = o.id
  7104.             AND (x.indid = 0 or x.indid = 1)    /*    If there are no indexes */
  7105.                                                 /*    then table stats are in */
  7106.                                                 /*    a row with indid =0        */
  7107.  
  7108.     SELECT
  7109.         TABLE_QUALIFIER,
  7110.         TABLE_OWNER,
  7111.         TABLE_NAME,
  7112.         NON_UNIQUE,
  7113.         INDEX_QUALIFIER,
  7114.         INDEX_NAME,
  7115.         TYPE,
  7116.         SEQ_IN_INDEX,
  7117.         COLUMN_NAME,
  7118.         COLLATION,
  7119.         CARDINALITY,
  7120.         PAGES,
  7121.         FILTER_CONDITION = convert(varchar(128),null)
  7122.     FROM #TmpIndex
  7123.     WHERE
  7124.         INDEX_NAME like @index_name /* If matching name */
  7125.         or INDEX_NAME is null        /* If SQL_TABLE_STAT row */
  7126.     ORDER BY 4, 7, 6, 8
  7127.  
  7128.     DROP TABLE #TmpIndex, #TmpColumns
  7129. go
  7130.  
  7131. grant execute on sp_statistics to public
  7132. go
  7133.  
  7134. dump tran master with no_log
  7135. go
  7136.  
  7137. print 'creating sp_stored_procedures'
  7138. go
  7139.  
  7140.  
  7141. /* pre 8.0 version */
  7142. create procedure sp_stored_procedures(
  7143.                         @sp_name        varchar(102) = null,
  7144.                         @sp_owner        varchar(96) = null,
  7145.                         @sp_qualifier    varchar(32) = null)
  7146. as
  7147.     declare @proc_type smallint
  7148.  
  7149.     if @sp_qualifier is not null
  7150.     begin
  7151.         if db_name() <> @sp_qualifier
  7152.         begin
  7153.             if @sp_qualifier = ''
  7154.             begin
  7155.                 /* in this case, we need to return an empty result set */
  7156.                 /* because the user has requested a database with an empty name */
  7157.                 select @sp_name = ''
  7158.                 select @sp_owner = ''
  7159.             end else
  7160.             begin    /* If qualifier doesn't match current database */
  7161.                 raiserror 20001 '~~Rush_51~~'
  7162.                 return
  7163.             end
  7164.         end
  7165.     end
  7166.  
  7167.     if @sp_name is null
  7168.     begin  /*  If procedure name not supplied, match all */
  7169.         select @sp_name = '%'
  7170.     end
  7171.     else begin
  7172.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7173.         begin
  7174.             if exists (select * from sysobjects
  7175.                 where uid = user_id()
  7176.                     and name = @sp_name
  7177.                     and type = 'P') /* Object type of Procedure */
  7178.             begin
  7179.                 select @sp_owner = user_name()
  7180.             end
  7181.         end
  7182.     end
  7183.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  7184.         select @sp_owner = '%'
  7185.  
  7186.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  7187.  
  7188.     select
  7189.         PROCEDURE_QUALIFIER = convert(varchar(32),db_name()),
  7190.         PROCEDURE_OWNER = convert(varchar(32),user_name(o.uid)),
  7191.         PROCEDURE_NAME = convert(varchar(36),o.name +';'+ ltrim(str(c.number,5))),
  7192.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  7193.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7194.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  7195.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  7196.         PROCEDURE_TYPE = @proc_type
  7197.     from
  7198.         sysobjects o,syscomments c,sysusers u
  7199.     where
  7200.         o.name like @sp_name
  7201.         and c.colid = 1
  7202.         and user_name(o.uid) like @sp_owner
  7203.         and o.type = 'P'        /* Object type of Procedure */
  7204.         and c.id = o.id
  7205.         and u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  7206.         and (suser_id() = 1     /* User is the System Administrator */
  7207.             or o.uid = user_id()    /* User created the object */
  7208.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  7209.             or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  7210.              from sysprotects p
  7211.              /* outer join to correlate with all rows in sysobjects */
  7212.              where p.id =* o.id
  7213.                  /*  get rows for public,current user,user's group */
  7214.                  and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  7215.                  /* check for SELECT,EXECUTE privilege */
  7216.                  and (action in (193,224)))&1    /* more magic...normalize GRANT */
  7217.             ) = 1     /* final magic...compare Grants    */
  7218.         )
  7219.     order by 1, 2, 3
  7220. go
  7221.  
  7222. grant execute on sp_stored_procedures to public
  7223. go
  7224.  
  7225.  
  7226. if (charindex('7.00', @@version) > 0 or
  7227.     charindex('8.00', @@version) > 0)
  7228.     drop procedure sp_stored_procedures
  7229. else
  7230. begin
  7231.     print ''
  7232.     print ''
  7233.     print 'Warning:'
  7234.     print 'you are installing the stored procedures '
  7235.     print 'on a pre 7.0 SQL Server.'
  7236.     print 'Ignore the following errors.'
  7237. end
  7238. go
  7239.  
  7240.  
  7241. /* 7.0 version */
  7242. create procedure sp_stored_procedures(
  7243.                         @sp_name        nvarchar(390) = null,
  7244.                         @sp_owner        nvarchar(384) = null,
  7245.                         @sp_qualifier    sysname = null)
  7246. as
  7247.     declare @proc_type smallint
  7248.  
  7249.     if @sp_qualifier is not null
  7250.     begin
  7251.         if db_name() <> @sp_qualifier
  7252.         begin
  7253.             if @sp_qualifier = ''
  7254.             begin
  7255.                 /* in this case, we need to return an empty result set */
  7256.                 /* because the user has requested a database with an empty name */
  7257.                 select @sp_name = ''
  7258.                 select @sp_owner = ''
  7259.             end else
  7260.             begin    /* If qualifier doesn't match current database */
  7261.                 raiserror (15250, -1,-1)
  7262.                 return
  7263.             end
  7264.         end
  7265.     end
  7266.  
  7267.     if @sp_name is null
  7268.     begin  /*  If procedure name not supplied, match all */
  7269.         select @sp_name = '%'
  7270.     end
  7271.     else begin
  7272.         if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7273.         begin
  7274.             if exists (select * from sysobjects
  7275.                 where uid = user_id()
  7276.                     and name = @sp_name
  7277.                     and type = 'P') /* Object type of Procedure */
  7278.             begin
  7279.                 select @sp_owner = user_name()
  7280.             end
  7281.         end
  7282.     end
  7283.     if @sp_owner is null    /*    If procedure owner not supplied, match all */
  7284.         select @sp_owner = '%'
  7285.  
  7286.     select @proc_type=2        /* Return 2 for 4.2 and later servers. */
  7287.  
  7288.     select
  7289.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7290.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7291.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7292.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  7293.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7294.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  7295.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  7296.         PROCEDURE_TYPE = @proc_type
  7297.     from
  7298.         sysobjects o,syscomments c
  7299.     where
  7300.         o.name like @sp_name
  7301.         and c.colid = 1
  7302.         and user_name(o.uid) like @sp_owner
  7303.         and o.type = 'P'        /* Object type of Procedure */
  7304.         and c.id = o.id
  7305.         and permissions (o.id)&32 <> 0
  7306.     order by 1, 2, 3
  7307. go
  7308.  
  7309. grant execute on sp_stored_procedures to public
  7310. go
  7311.  
  7312. if (charindex('8.00', @@version) > 0)
  7313.     drop procedure sp_stored_procedures
  7314. else
  7315. begin
  7316.     print ''
  7317.     print ''
  7318.     print 'Warning:'
  7319.     print 'you are installing the stored procedures '
  7320.     print 'on a pre 8.0 SQL Server.'
  7321.     print 'Ignore the following errors.'
  7322. end
  7323. go
  7324.  
  7325.  
  7326. /* 8.0 version */
  7327. create procedure sp_stored_procedures(
  7328.                         @sp_name    nvarchar(390) = null,
  7329.                         @sp_owner    nvarchar(384) = null,
  7330.                         @sp_qualifier    sysname = null)
  7331. as
  7332.     declare @proc_type smallint
  7333.  
  7334.     if @sp_qualifier is not null
  7335.     begin
  7336.         if db_name() <> @sp_qualifier
  7337.         begin
  7338.             if @sp_qualifier = ''
  7339.             begin
  7340.                 /* in this case, we need to return an empty result set */
  7341.                 /* because the user has requested a database with an empty name */
  7342.                 select @sp_name = ''
  7343.                 select @sp_owner = ''
  7344.             end else
  7345.             begin    /* If qualifier doesn't match current database */
  7346.                 raiserror (15250, -1,-1)
  7347.                 return
  7348.             end
  7349.         end
  7350.     end
  7351.  
  7352.     if @sp_name is null
  7353.     begin  /*  If procedure name not supplied, match all */
  7354.         select @sp_name = '%'
  7355.     end
  7356.     else
  7357.     begin
  7358.     if (@sp_owner is null) and (charindex('%', @sp_name) = 0)
  7359.     begin
  7360.         if exists (select * from sysobjects
  7361.             where uid = user_id()
  7362.                 and name = @sp_name
  7363.                 and type in ('P', 'FN', 'TF', 'IF'))        /* Procedures, scalar UDF, table UDF */
  7364.         begin
  7365.             select @sp_owner = user_name()
  7366.         end
  7367.     end
  7368.     end
  7369.     
  7370.     if @sp_owner is null        /* If procedure owner not supplied, match all */
  7371.     select @sp_owner = '%'
  7372.  
  7373.     select @proc_type=2            /* Return 2 for 4.2 and later servers. */
  7374.  
  7375.     select
  7376.         PROCEDURE_QUALIFIER = convert(sysname,db_name()),
  7377.         PROCEDURE_OWNER = convert(sysname,user_name(o.uid)),
  7378.         PROCEDURE_NAME = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  7379.         NUM_INPUT_PARAMS = -1,    /* Constant since value unknown */
  7380.         NUM_OUTPUT_PARAMS = -1, /* Constant since value unknown */
  7381.         NUM_RESULT_SETS = -1,    /* Constant since value unknown */
  7382.         REMARKS = convert(varchar(254),null),    /* Remarks are NULL */
  7383.         PROCEDURE_TYPE = @proc_type
  7384.     from
  7385.         sysobjects o,syscomments c
  7386.     where
  7387.         o.name like @sp_name
  7388.         and c.colid = 1
  7389.         and user_name(o.uid) like @sp_owner
  7390.         and o.type in ('P', 'FN', 'TF', 'IF')        /* Object type of Procedure, scalar UDF, table UDF */
  7391.         and c.id = o.id
  7392.         and permissions (o.id)&32 <> 0
  7393.     order by 1, 2, 3
  7394. go
  7395.  
  7396. grant execute on sp_stored_procedures to public
  7397. go
  7398.  
  7399. dump tran master with no_log
  7400. go
  7401.  
  7402.  
  7403. print 'creating sp_table_privileges'
  7404. go
  7405.  
  7406. /*    Procedure for pre 6.50 server */
  7407. CREATE PROCEDURE sp_table_privileges (
  7408.             @table_name         varchar(90),
  7409.             @table_owner        varchar(90) = null,
  7410.             @table_qualifier    varchar(32) = null)
  7411. as
  7412.     set nocount on
  7413.  
  7414.     declare @table_id    int,
  7415.             @owner_id     int,
  7416.             @full_table_name char(181)
  7417.     declare @refconst int
  7418.  
  7419.     select @refconst = 1
  7420.     if    (charindex('6.00', @@version) = 0)
  7421.         select @refconst = NULL
  7422.  
  7423.     if @table_qualifier is not null
  7424.     begin
  7425.         if db_name() != @table_qualifier
  7426.         begin    /* If qualifier doesn't match current database */
  7427.             raiserror 20001 'Table qualifier must be name of current database'
  7428.             return
  7429.         end
  7430.     end
  7431.     if @table_owner is null
  7432.     begin    /* If unqualified table name */
  7433.         SELECT @full_table_name = @table_name
  7434.     end
  7435.     else
  7436.     begin    /* Qualified table name */
  7437.         SELECT @full_table_name = @table_owner + '.' + @table_name
  7438.     end
  7439.     /*    Get Object ID */
  7440.     SELECT @table_id = object_id(@full_table_name)
  7441.  
  7442.     if @@trancount != 0
  7443.     begin    /* If inside a transaction */
  7444.         raiserror 20003 'The procedure ''sp_table_privileges'' cannot be executed from within a transaction.'
  7445.         return
  7446.     end
  7447.     create table #table_priv1(
  7448.         table_qualifier            varchar(32) NOT NULL,
  7449.         table_owner             varchar(32) NOT NULL,
  7450.         table_name                varchar(32) NOT NULL,
  7451.         grantor                 varchar(32) NOT NULL,
  7452.         grantee                 varchar(32) NOT NULL,
  7453.         select_privilege        int NOT NULL,
  7454.         insert_privilege        int NOT NULL,
  7455.         update_privilege        int NOT NULL,
  7456.         delete_privilege        int NOT NULL,
  7457.         references_privilege    int NULL,
  7458.         is_grantable            varchar(3) NOT NULL,
  7459.         uid                     int NOT NULL,
  7460.         gid                     int NOT NULL)
  7461.  
  7462.     insert into #table_priv1
  7463.         select distinct
  7464.             db_name(),
  7465.             user_name(o.uid),
  7466.             o.name,
  7467.             user_name(o.uid),
  7468.             u.name,
  7469.             0,
  7470.             0,
  7471.             0,
  7472.             0,
  7473.             @refconst,
  7474.             'no',
  7475.             u.uid,
  7476.             u.gid
  7477.         from sysusers u, sysobjects o
  7478.         where o.id = @table_id and u.uid != u.gid
  7479.            and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  7480.                                            ** user tables, and views. */
  7481.  
  7482.     /*
  7483.     ** now add row for table owner
  7484.     */
  7485.     if exists (
  7486.         select *
  7487.             from #table_priv1
  7488.             where grantor = grantee)
  7489.     begin
  7490.         update #table_priv1
  7491.         set
  7492.             select_privilege = 1,
  7493.             update_privilege = 1,
  7494.             insert_privilege = 1,
  7495.             delete_privilege = 1,
  7496.             references_privilege = 1,
  7497.             is_grantable = 'yes'
  7498.         where grantor = grantee
  7499.     end
  7500.     else
  7501.     begin
  7502.         insert into #table_priv1
  7503.             select    db_name(),
  7504.                 user_name(o.uid),
  7505.                 o.name,
  7506.                 user_name(o.uid),
  7507.                 user_name(o.uid),
  7508.                 1,
  7509.                 1,
  7510.                 1,
  7511.                 1,
  7512.                 @refconst,
  7513.                 'yes',
  7514.                 o.uid,
  7515.                 u.gid
  7516.             from sysobjects o, sysusers u
  7517.             where o.id = @table_id and u.uid = o.uid
  7518.             and sysstat & 0xf in (1,2,3)    /* only valid for system tables,
  7519.                                            ** user tables, and views. */
  7520.  
  7521.     end
  7522.  
  7523.     update #table_priv1
  7524.     set select_privilege = 1
  7525.     where
  7526.         exists (
  7527.             select * from sysprotects
  7528.             where
  7529.                 id = @table_id
  7530.                 and (#table_priv1.uid = uid
  7531.                     or #table_priv1.gid = uid
  7532.                     or uid = 0)
  7533.                 and protecttype = 205
  7534.                 and action = 193)
  7535.         and not exists (
  7536.             select * from sysprotects
  7537.             where
  7538.                 id = @table_id
  7539.                 and (#table_priv1.uid = uid
  7540.                     or #table_priv1.gid = uid
  7541.                     or uid = 0)
  7542.                 and protecttype = 206
  7543.                 and action = 193)
  7544.  
  7545.     update #table_priv1
  7546.     set insert_privilege = 1
  7547.     where
  7548.         exists (
  7549.             select * from sysprotects
  7550.             where
  7551.                 id = @table_id
  7552.                 and (#table_priv1.uid = uid
  7553.                     or #table_priv1.gid = uid
  7554.                     or uid = 0)
  7555.                 and protecttype = 205
  7556.                 and action = 195)
  7557.         and not exists (
  7558.             select * from sysprotects
  7559.             where
  7560.                 id = @table_id
  7561.                 and (#table_priv1.uid = uid
  7562.                     or #table_priv1.gid = uid
  7563.                     or uid = 0)
  7564.                 and protecttype = 206
  7565.                 and action = 195)
  7566.  
  7567.     update #table_priv1
  7568.     set delete_privilege = 1
  7569.     where
  7570.         exists (
  7571.             select * from sysprotects
  7572.             where
  7573.                 id = @table_id
  7574.                 and (#table_priv1.uid = uid
  7575.                     or #table_priv1.gid = uid
  7576.                     or uid = 0)
  7577.                 and protecttype = 205
  7578.                 and action = 196)
  7579.         and not exists (select * from sysprotects
  7580.             where
  7581.                 id = @table_id
  7582.                 and (#table_priv1.uid = uid
  7583.                     or #table_priv1.gid = uid
  7584.                     or uid = 0)
  7585.                 and protecttype = 206
  7586.                 and action = 196)
  7587.  
  7588.     update #table_priv1
  7589.     set update_privilege = 1
  7590.     where
  7591.         exists (
  7592.             select * from sysprotects
  7593.             where
  7594.                 id = @table_id
  7595.                 and (#table_priv1.uid = uid
  7596.                     or #table_priv1.gid = uid
  7597.                     or uid = 0)
  7598.                 and protecttype = 205
  7599.                 and action = 197)
  7600.         and not exists (
  7601.             select * from sysprotects
  7602.             where
  7603.                 id = @table_id
  7604.                 and (#table_priv1.uid = uid
  7605.                     or #table_priv1.gid = uid
  7606.                     or uid = 0)
  7607.                 and protecttype = 206
  7608.                 and action = 197)
  7609.  
  7610.     update #table_priv1
  7611.     set references_privilege = 1
  7612.     where
  7613.         exists (
  7614.             select * from sysprotects
  7615.             where
  7616.                 id = @table_id
  7617.                 and (#table_priv1.uid = uid
  7618.                     or #table_priv1.gid = uid
  7619.                     or uid = 0)
  7620.                 and protecttype = 205
  7621.                 and action = 26)
  7622.         and not exists (
  7623.             select * from sysprotects
  7624.             where
  7625.                 id = @table_id
  7626.                 and (#table_priv1.uid = uid
  7627.                     or #table_priv1.gid = uid
  7628.                     or uid = 0)
  7629.                 and protecttype = 206
  7630.                 and action = 26)
  7631.  
  7632.     create table #table_priv2(
  7633.         table_qualifier varchar(32) NULL,
  7634.         table_owner     varchar(32) NULL,
  7635.         table_name        varchar(32) NOT NULL,
  7636.         grantor         varchar(32) NULL,
  7637.         grantee         varchar(32) NOT NULL,
  7638.         privilege        varchar(32) NOT NULL,
  7639.         is_grantable    varchar(3) NULL)
  7640.  
  7641.     insert into #table_priv2
  7642.         select
  7643.             table_qualifier,
  7644.             table_owner,
  7645.             table_name,
  7646.             grantor,
  7647.             grantee,
  7648.             'SELECT',
  7649.             is_grantable
  7650.         from #table_priv1
  7651.         where select_privilege = 1
  7652.  
  7653.  
  7654.     insert into #table_priv2
  7655.         select
  7656.             table_qualifier,
  7657.             table_owner,
  7658.             table_name,
  7659.             grantor,
  7660.             grantee,
  7661.             'INSERT',
  7662.             is_grantable
  7663.         from #table_priv1
  7664.         where insert_privilege = 1
  7665.  
  7666.  
  7667.     insert into #table_priv2
  7668.         select
  7669.             table_qualifier,
  7670.             table_owner,
  7671.             table_name,
  7672.             grantor,
  7673.             grantee,
  7674.             'DELETE',
  7675.             is_grantable
  7676.         from #table_priv1
  7677.         where delete_privilege = 1
  7678.  
  7679.  
  7680.     insert into #table_priv2
  7681.         select
  7682.             table_qualifier,
  7683.             table_owner,
  7684.             table_name,
  7685.             grantor,
  7686.             grantee,
  7687.             'UPDATE',
  7688.             is_grantable
  7689.         from #table_priv1
  7690.         where update_privilege = 1
  7691.  
  7692.     insert into #table_priv2
  7693.         select
  7694.             table_qualifier,
  7695.             table_owner,
  7696.             table_name,
  7697.             grantor,
  7698.             grantee,
  7699.             'REFERENCES',
  7700.             is_grantable
  7701.         from #table_priv1
  7702.         where references_privilege = 1
  7703.  
  7704.  
  7705.     select * from #table_priv2
  7706.     order by table_owner,table_name,privilege,grantee
  7707. /*    order by 2,3,6,5 Can't use since fails on 4.21a server */
  7708. go
  7709.  
  7710. if (charindex('6.50', @@version) = 0 and
  7711.     charindex('7.00', @@version) = 0 and
  7712.     charindex('8.00', @@version) = 0)
  7713. begin
  7714.     print ''
  7715.     print ''
  7716.     print 'Warning:'
  7717.     print 'you are installing the stored procedures '
  7718.     print 'on a pre 6.50 SQL Server.'
  7719.     print 'Ignore the following errors.'
  7720. end
  7721. else
  7722.     drop proc sp_table_privileges
  7723. go
  7724.  
  7725.  
  7726. /*    Procedure for 6.50 server */
  7727. CREATE PROCEDURE sp_table_privileges (
  7728.             @table_name         varchar(96),
  7729.             @table_owner        varchar(96) = null,
  7730.             @table_qualifier    varchar(32) = null)
  7731. as
  7732.  
  7733.     if @table_qualifier is not null
  7734.     begin
  7735.         if db_name() <> @table_qualifier
  7736.         begin    /* If qualifier doesn't match current database */
  7737.             raiserror (15250, -1,-1)
  7738.             return
  7739.         end
  7740.     end
  7741.     if @table_name is null
  7742.         select @table_name = '%'
  7743.     if @table_owner is null /* If no owner supplied, force wildcard */
  7744.         select @table_owner = '%'
  7745.  
  7746.     select
  7747.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7748.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7749.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7750.         convert(varchar(32),user_name(p.grantor)) GRANTOR,
  7751.         convert(varchar(32),user_name(u.uid)) GRANTEE,
  7752.         convert(varchar(32),case p.action
  7753.              when 193 then 'SELECT'
  7754.              when 195 then 'INSERT'
  7755.              when 196 then 'DELETE'
  7756.              when 197 then 'UPDATE'
  7757.              else 'REFERENCES'
  7758.         end) PRIVILEGE,
  7759.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7760.             else 'YES'
  7761.         end) IS_GRANTABLE
  7762.     from sysprotects p, sysobjects o, sysusers u
  7763.     where
  7764.         p.id = o.id
  7765.         and o.type in ('U','V','S')
  7766.         and object_name(o.id) like @table_name
  7767.         and user_name(o.uid) like @table_owner
  7768.             /* expand groups */
  7769.         and ((p.uid = u.uid and u.uid <> u.gid) or
  7770.              (p.uid = u.gid and u.uid <> u.gid))
  7771.         and p.protecttype <> 206    /* only grant rows */
  7772.         and p.action in (26,193,195,196,197)
  7773.         and o.uid <> u.uid            /* no rows for owner */
  7774.         and not exists (            /* exclude revoke'd privileges */
  7775.             select *
  7776.             from sysprotects p1
  7777.             where
  7778.                 p1.protecttype = 206
  7779.                 and p1.action = p.action
  7780.                 and p1.id = p.id
  7781.                 and p1.uid = u.uid)
  7782.     union
  7783.     select    /*    Add rows for table owner */
  7784.         convert(varchar(32),db_name()) TABLE_QUALIFIER,
  7785.         convert(varchar(32),user_name(o.uid)) TABLE_OWNER,
  7786.         convert(varchar(32),object_name(o.id)) TABLE_NAME,
  7787.         convert(varchar(32),user_name(u.uid)) GRANTOR,
  7788.         convert(varchar(32),user_name(o.uid)) GRANTEE,
  7789.         convert(varchar(32),case v.number
  7790.             when 193 then 'SELECT'
  7791.             when 195 then 'INSERT'
  7792.             when 196 then 'DELETE'
  7793.             when 197 then 'UPDATE'
  7794.             else 'REFERENCES'
  7795.         end) PRIVILEGE,
  7796.         convert(varchar(3),'YES') IS_GRANTABLE
  7797.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7798.     where
  7799.         object_name(o.id) like @table_name
  7800.         and o.type in ('U','V','S')
  7801.         and user_name(o.uid) like @table_owner
  7802.         and u.uid = 1        /* grantor is dbo of database */
  7803.         and v.type = 'P'    /* cross product to get all exposed privileges */
  7804.         and v.number in (26,193,195,196,197)
  7805.         and not exists (    /* exclude revoke'd privileges */
  7806.             select *
  7807.             from sysprotects p1
  7808.             where
  7809.                 p1.protecttype = 206
  7810.                 and p1.action = v.number
  7811.                 and p1.id = o.id
  7812.                 and p1.uid = o.uid)
  7813.     order by 2,3,6,5
  7814. go
  7815.  
  7816.  
  7817. if (charindex('7.00', @@version) > 0 or
  7818.     charindex('8.00', @@version) > 0)
  7819.     drop proc sp_table_privileges
  7820. else
  7821. begin
  7822.     print ''
  7823.     print ''
  7824.     print 'Warning:'
  7825.     print 'you are installing the stored procedures '
  7826.     print 'on a pre 7.0 SQL Server.'
  7827.     print 'Ignore the following errors.'
  7828. end
  7829. go
  7830.  
  7831. /*    Procedure for 7.0 and later servers */
  7832. CREATE PROCEDURE sp_table_privileges (
  7833.             @table_name         nvarchar(384),
  7834.             @table_owner        nvarchar(384) = null,
  7835.             @table_qualifier    sysname = null)
  7836. as
  7837.  
  7838.     if @table_qualifier is not null
  7839.     begin
  7840.         if db_name() <> @table_qualifier
  7841.         begin    /* If qualifier doesn't match current database */
  7842.             raiserror (15250, -1,-1)
  7843.             return
  7844.         end
  7845.     end
  7846.     if @table_name is null
  7847.         select @table_name = '%'
  7848.     if @table_owner is null /* If no owner supplied, force wildcard */
  7849.         select @table_owner = '%'
  7850.  
  7851.     select
  7852.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7853.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7854.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7855.         convert(sysname,user_name(p.grantor)) GRANTOR,
  7856.         convert(sysname,user_name(u.uid)) GRANTEE,
  7857.         convert(varchar(32),case p.action
  7858.              when 193 then 'SELECT'
  7859.              when 195 then 'INSERT'
  7860.              when 196 then 'DELETE'
  7861.              when 197 then 'UPDATE'
  7862.              else 'REFERENCES'
  7863.         end) PRIVILEGE,
  7864.         convert(varchar(3),case when p.protecttype = 205 then 'NO'
  7865.             else 'YES'
  7866.         end) IS_GRANTABLE
  7867.     from sysprotects p, sysobjects o, sysusers u, sysmembers m
  7868.     where
  7869.         p.id = o.id
  7870.         and o.type in ('U','V','S')
  7871.         and object_name(o.id) like @table_name
  7872.         and user_name(o.uid) like @table_owner
  7873.             /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  7874.         and (u.uid > 0 and u.uid < 16384)
  7875.         and ((p.uid = u.uid) or
  7876.              (p.uid = m.groupuid and u.uid = m.memberuid))
  7877.         and p.protecttype <> 206    /* only grant rows */
  7878.         and p.action in (26,193,195,196,197)
  7879.         and o.uid <> u.uid            /* no rows for owner */
  7880.         and not exists (            /* exclude revoke'd privileges */
  7881.             select *
  7882.             from sysprotects p1
  7883.             where
  7884.                 p1.protecttype = 206
  7885.                 and p1.action = p.action
  7886.                 and p1.id = p.id
  7887.                 and p1.uid = u.uid)
  7888.     union
  7889.     select    /*    Add rows for table owner */
  7890.         convert(sysname,db_name()) TABLE_QUALIFIER,
  7891.         convert(sysname,user_name(o.uid)) TABLE_OWNER,
  7892.         convert(sysname,object_name(o.id)) TABLE_NAME,
  7893.         convert(sysname,user_name(u.uid)) GRANTOR,
  7894.         convert(sysname,user_name(o.uid)) GRANTEE,
  7895.         convert(varchar(32),case v.number
  7896.             when 193 then 'SELECT'
  7897.             when 195 then 'INSERT'
  7898.             when 196 then 'DELETE'
  7899.             when 197 then 'UPDATE'
  7900.             else 'REFERENCES'
  7901.         end) PRIVILEGE,
  7902.         convert(varchar(3),'YES') IS_GRANTABLE
  7903.     from sysobjects o, master.dbo.spt_values v, sysusers u
  7904.     where
  7905.         object_name(o.id) like @table_name
  7906.         and o.type in ('U','V','S')
  7907.         and user_name(o.uid) like @table_owner
  7908.         and u.uid = 1        /* grantor is 'dbo' of database */
  7909.         and v.type = N'P'    /* cross product to get all exposed privileges */
  7910.         and v.number in (26,193,195,196,197)
  7911.         and not exists (    /* exclude revoke'd privileges */
  7912.             select *
  7913.             from sysprotects p1
  7914.             where
  7915.                 p1.protecttype = 206
  7916.                 and p1.action = v.number
  7917.                 and p1.id = o.id
  7918.                 and p1.uid = o.uid)
  7919.     order by 2,3,6,5
  7920. go
  7921.  
  7922. grant execute on sp_table_privileges to public
  7923. go
  7924.  
  7925. dump tran master with no_log
  7926. go
  7927.  
  7928. print 'creating sp_tables'
  7929. go
  7930.  
  7931. /*    Procedure for 6.50 and earlier servers */
  7932. create procedure sp_tables(
  7933.                @table_name        varchar(96)    = null,
  7934.                @table_owner     varchar(96)    = null,
  7935.                @table_qualifier varchar(32)    = null,
  7936.                @table_type        varchar(100) = null)
  7937. as
  7938.     declare @type1 varchar(3)
  7939.     declare @tableindex int
  7940.  
  7941.  
  7942.     /* Special feature #1:    enumerate databases when owner and name
  7943.          are blank but qualifier is explicitly '%'.  */
  7944.     if @table_qualifier = '%' and
  7945.         @table_owner = '' and
  7946.         @table_name = ''
  7947.     begin    /* If enumerating databases */
  7948.         select
  7949.             TABLE_QUALIFIER = convert(varchar(32),d.name),
  7950.             TABLE_OWNER = convert(varchar(32),null),
  7951.             TABLE_NAME = convert(varchar(32),null),
  7952.             TABLE_TYPE = convert(varchar(32),null),
  7953.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7954.         from master.dbo.sysdatabases d
  7955.         where d.name <> 'model'    /* eliminate MODEL database */
  7956.         order by 1
  7957.     end
  7958.  
  7959.     /* Special feature #2:    enumerate owners when qualifier and name
  7960.          are blank but owner is explicitly '%'.  */
  7961.     else if @table_qualifier = '' and
  7962.         @table_owner = '%' and
  7963.         @table_name = ''
  7964.     begin    /* If enumerating owners */
  7965.         select distinct
  7966.             TABLE_QUALIFIER = convert(varchar(32),null),
  7967.             TABLE_OWNER = convert(varchar(32),user_name(uid)),
  7968.             TABLE_NAME = convert(varchar(32),null),
  7969.             TABLE_TYPE = convert(varchar(32),null),
  7970.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7971.         from sysobjects
  7972.         order by 2
  7973.     end
  7974.  
  7975.     /* Special feature #3:    enumerate table types when qualifier, owner and
  7976.          name are blank but table type is explicitly '%'.    */
  7977.     else if @table_qualifier = '' and
  7978.         @table_owner = '' and
  7979.         @table_name = '' and
  7980.         @table_type = '%'
  7981.     begin    /* If enumerating table types */
  7982.         select
  7983.             TABLE_QUALIFIER = convert(varchar(32),null),
  7984.             TABLE_OWNER = convert(varchar(32),null),
  7985.             TABLE_NAME = convert(varchar(32),null),
  7986.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  7987.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  7988.         from sysobjects o, syscolumns c
  7989.         where o.id=c.id and o.name='sysusers' and colid<=3
  7990.     end
  7991.  
  7992.     else
  7993.     begin /* end of special features - do normal processing */
  7994.         if @table_qualifier is not null
  7995.         begin
  7996.             if db_name() <> @table_qualifier
  7997.             begin
  7998.                 if @table_qualifier = ''
  7999.                 begin  /* If empty qualifier supplied */
  8000.                     /* Force an empty result set */
  8001.                     select @table_name = ''
  8002.                     select @table_owner = ''
  8003.                 end
  8004.                 else
  8005.                 begin    /* If qualifier doesn't match current database */
  8006.                     raiserror 20001 '~~Rush_5~~'
  8007.                     return
  8008.                 end
  8009.             end
  8010.         end
  8011.         if @table_type is null
  8012.         begin    /* Select all ODBC supported table types */
  8013.             select @type1 = 'SUV'
  8014.         end
  8015.         else
  8016.         begin
  8017.             /*    TableType is case sensitive if CS server */
  8018.             select @type1 = null
  8019.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8020.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8021.             if (charindex('''TABLE''',@table_type) <> 0)
  8022.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8023.             if (charindex('''VIEW''',@table_type) <> 0)
  8024.                 select @type1 = @type1 + 'V'    /* Add Views */
  8025.         end
  8026.         if @table_name is null
  8027.         begin    /*    If table name not supplied, match all */
  8028.             select @table_name = '%'
  8029.         end
  8030.         else
  8031.         begin
  8032.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8033.             begin    /* If owner not specified and table is specified */
  8034.                 if exists (select * from sysobjects
  8035.                     where uid = user_id()
  8036.                     and name = @table_name
  8037.                     and (type = 'U' or type = 'V' or type = 'S'))
  8038.                 begin    /* Override supplied owner w/owner of table */
  8039.                     select @table_owner = user_name()
  8040.                 end
  8041.             end
  8042.         end
  8043.         if @table_owner is null /* If no owner supplied, force wildcard */
  8044.             select @table_owner = '%'
  8045.         select
  8046.             TABLE_QUALIFIER = convert(varchar(32),db_name()),
  8047.             TABLE_OWNER = convert(varchar(32),user_name(o.uid)),
  8048.             TABLE_NAME = convert(varchar(32),o.name),    /* make nullable */
  8049.             TABLE_TYPE = convert(varchar(32),rtrim(
  8050.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8051.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  8052.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8053.         from sysusers u, sysobjects o
  8054.         where
  8055.             o.name like @table_name
  8056.             and user_name(o.uid) like @table_owner
  8057.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8058.             and u.uid = user_id() /* constrain sysusers uid for use in subquery */
  8059.             and (
  8060.                 suser_id() = 1     /* User is the System Administrator */
  8061.                 or o.uid = user_id()     /* User created the object */
  8062.                 /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  8063.                 or ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  8064.                     from sysprotects p
  8065.                     /* outer join to correlate with all rows in sysobjects */
  8066.                     where p.id =* o.id
  8067.                         /* get rows for public,current user,user's group */
  8068.                         and (p.uid = 0 or p.uid = user_id() or p.uid =* u.gid)
  8069.                         /* check for SELECT,EXECUTE privilege */
  8070.                         and (action in (193,224)))&1     /* more magic...normalize GRANT */
  8071.                     ) = 1    /* final magic...compare Grants      */
  8072.             )
  8073.         order by 4, 1, 2, 3
  8074.     end
  8075. go
  8076.  
  8077. if (charindex('7.00', @@version) > 0 or
  8078.     charindex('8.00', @@version) > 0)
  8079.     drop procedure sp_tables
  8080. else
  8081. begin
  8082.     print ''
  8083.     print ''
  8084.     print 'Warning:'
  8085.     print 'you are installing the stored procedures '
  8086.     print 'on a pre 8.0 SQL Server.'
  8087.     print 'Ignore the following errors.'
  8088. end
  8089. go
  8090.  
  8091. /*    Procedure for 8.0 server */
  8092. create procedure sp_tables(
  8093.                @table_name        nvarchar(384)    = null,
  8094.                @table_owner     nvarchar(384)    = null,
  8095.                @table_qualifier sysname    = null,
  8096.                @table_type        varchar(100) = null)
  8097. as
  8098.     declare @type1 varchar(3)
  8099.     declare @tableindex int
  8100.  
  8101.  
  8102.     /* Special feature #1:    enumerate databases when owner and name
  8103.          are blank but qualifier is explicitly '%'.  */
  8104.     if @table_qualifier = '%' and
  8105.         @table_owner = '' and
  8106.         @table_name = ''
  8107.     begin    /* If enumerating databases */
  8108.         select
  8109.             TABLE_QUALIFIER = convert(sysname,d.name),
  8110.             TABLE_OWNER = convert(sysname,null),
  8111.             TABLE_NAME = convert(sysname,null),
  8112.             TABLE_TYPE = convert(varchar(32),null),
  8113.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8114.         from master.dbo.sysdatabases d
  8115.         where d.name <> 'model'    /* eliminate MODEL database */
  8116.         order by 1
  8117.     end
  8118.  
  8119.     /* Special feature #2:    enumerate owners when qualifier and name
  8120.          are blank but owner is explicitly '%'.  */
  8121.     else if @table_qualifier = '' and
  8122.         @table_owner = '%' and
  8123.         @table_name = ''
  8124.     begin    /* If enumerating owners */
  8125.         select distinct
  8126.             TABLE_QUALIFIER = convert(sysname,null),
  8127.             TABLE_OWNER = convert(sysname,user_name(uid)),
  8128.             TABLE_NAME = convert(sysname,null),
  8129.             TABLE_TYPE = convert(varchar(32),null),
  8130.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8131.         from sysobjects
  8132.         order by 2
  8133.     end
  8134.  
  8135.     /* Special feature #3:    enumerate table types when qualifier, owner and
  8136.          name are blank but table type is explicitly '%'.    */
  8137.     else if @table_qualifier = '' and
  8138.         @table_owner = '' and
  8139.         @table_name = '' and
  8140.         @table_type = '%'
  8141.     begin    /* If enumerating table types */
  8142.         select
  8143.             TABLE_QUALIFIER = convert(sysname,null),
  8144.             TABLE_OWNER = convert(sysname,null),
  8145.             TABLE_NAME = convert(sysname,null),
  8146.             TABLE_TYPE = convert(varchar(32),rtrim(substring('SYSTEM TABLETABLE       VIEW',(colid-1)*12+1,12))),
  8147.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8148.         from sysobjects o, syscolumns c
  8149.         where o.id=c.id and o.name='sysusers' and colid<=3
  8150.     end
  8151.  
  8152.     else
  8153.     begin /* end of special features - do normal processing */
  8154.         if @table_qualifier is not null
  8155.         begin
  8156.             if db_name() <> @table_qualifier
  8157.             begin
  8158.                 if @table_qualifier = ''
  8159.                 begin  /* If empty qualifier supplied */
  8160.                     /* Force an empty result set */
  8161.                     select @table_name = ''
  8162.                     select @table_owner = ''
  8163.                 end
  8164.                 else
  8165.                 begin    /* If qualifier doesn't match current database */
  8166.                     raiserror (15250, -1,-1)
  8167.                     return
  8168.                 end
  8169.             end
  8170.         end
  8171.         if @table_type is null
  8172.         begin    /* Select all ODBC supported table types */
  8173.             select @type1 = 'SUV'
  8174.         end
  8175.         else
  8176.         begin
  8177.             /*    TableType is case sensitive if CS server */
  8178.             select @type1 = ''
  8179.             if (charindex('''SYSTEM TABLE''',@table_type) <> 0)
  8180.                 select @type1 = @type1 + 'S'    /* Add System Tables */
  8181.             if (charindex('''TABLE''',@table_type) <> 0)
  8182.                 select @type1 = @type1 + 'U'    /* Add User Tables */
  8183.             if (charindex('''VIEW''',@table_type) <> 0)
  8184.                 select @type1 = @type1 + 'V'    /* Add Views */
  8185.         end
  8186.         if @table_name is null
  8187.         begin    /*    If table name not supplied, match all */
  8188.             select @table_name = '%'
  8189.         end
  8190.         else
  8191.         begin
  8192.             if (@table_owner is null) and (charindex('%', @table_name) = 0)
  8193.             begin    /* If owner not specified and table is specified */
  8194.                 if exists (select * from sysobjects
  8195.                     where uid = user_id()
  8196.                     and name = @table_name
  8197.                     and (type = 'U' or type = 'V' or type = 'S'))
  8198.                 begin    /* Override supplied owner w/owner of table */
  8199.                     select @table_owner = user_name()
  8200.                 end
  8201.             end
  8202.         end
  8203.         if @table_owner is null /* If no owner supplied, force wildcard */
  8204.             select @table_owner = '%'
  8205.         select
  8206.             TABLE_QUALIFIER = convert(sysname,db_name()),
  8207.             TABLE_OWNER = convert(sysname,user_name(o.uid)),
  8208.             TABLE_NAME = convert(sysname,o.name),    /* make nullable */
  8209.             TABLE_TYPE = convert(varchar(32),rtrim(
  8210.                 substring('SYSTEM TABLE            TABLE       VIEW       ',
  8211.                     (ascii(o.type)-83)*12+1,12))),    /* 'S'=0,'U'=2,'V'=3 */
  8212.             REMARKS = convert(varchar(254),null)    /* Remarks are NULL */
  8213.         from sysobjects o
  8214.         where
  8215.             o.name like @table_name
  8216.             and user_name(o.uid) like @table_owner
  8217.             and o.type in ('U','V','S')
  8218.             and charindex(substring(o.type,1,1),@type1) <> 0 /* Only desired types */
  8219.             and permissions (o.id)&4096 <> 0
  8220.         order by 4, 1, 2, 3
  8221.     end
  8222. go
  8223.  
  8224.  
  8225. grant execute on sp_tables to public
  8226. go
  8227.  
  8228. dump tran master with no_log
  8229. go
  8230.  
  8231. /*-----------------------------------------------------------------------------*/
  8232. /*-------------- CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  8233. /*-----------------------------------------------------------------------------*/
  8234.  
  8235. if object_id('sp_linkedservers', 'P') is not null
  8236.     drop proc sp_linkedservers
  8237. go
  8238. raiserror(15339,-1,-1,'sp_linkedservers')
  8239. go
  8240. create proc sp_linkedservers as
  8241.     select
  8242.         SRV_NAME = srvname,
  8243.         SRV_PROVIDERNAME = providername,
  8244.         SRV_PRODUCT = srvproduct,
  8245.         SRV_DATASOURCE = datasource,
  8246.         SRV_PROVIDERSTRING = providerstring,
  8247.         SRV_LOCATION = location,
  8248.         SRV_CAT = catalog
  8249.     from master.dbo.sysservers
  8250.     order by 1
  8251. go
  8252. grant execute on sp_linkedservers to public
  8253. go
  8254.  
  8255.  
  8256. if object_id('sp_catalogs', 'P') is not null
  8257.     drop proc sp_catalogs
  8258. go
  8259. raiserror(15339,-1,-1,'sp_catalogs')
  8260. go
  8261. create procedure sp_catalogs(
  8262.     @server_name        sysname)
  8263. as
  8264.     select
  8265.         CATALOG_NAME,
  8266.         convert (nvarchar(255),DESCRIPTION)
  8267.     from master.dbo.SYSREMOTE_CATALOGS < @server_name >
  8268.     order by CATALOG_NAME
  8269. go
  8270. grant execute on sp_catalogs to public
  8271. go
  8272.  
  8273.  
  8274. if object_id('sp_tables_ex', 'P') is not null
  8275.     drop proc sp_tables_ex
  8276. go
  8277. raiserror(15339,-1,-1,'sp_tables_ex')
  8278. go
  8279. create procedure sp_tables_ex(
  8280.     @table_server        sysname,
  8281.     @table_name            sysname = null,
  8282.     @table_schema        sysname = null,
  8283.     @table_catalog        sysname = null,
  8284.     @table_type            sysname = null)
  8285. as
  8286.     declare @table_catalog_param        sysname
  8287.     if ((isnull(charindex('%', @table_catalog),0) = 0) and
  8288.         (isnull(charindex('[', @table_catalog),0) = 0) and
  8289.         (isnull(charindex('_', @table_catalog),0) = 0))
  8290.         select @table_catalog_param = @table_catalog
  8291.     else
  8292.         select @table_catalog_param = null
  8293.  
  8294.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8295.         (isnull(charindex('[', @table_name),0) = 0) and
  8296.         (isnull(charindex('_', @table_name),0) = 0))
  8297.     begin    /*    If no wild carding */
  8298.         select
  8299.             TABLE_CAT = TABLE_CATALOG,
  8300.             TABLE_SCHEM = TABLE_SCHEMA,
  8301.             TABLE_NAME = TABLE_NAME,
  8302.             TABLE_TYPE = TABLE_TYPE,
  8303.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8304.         from master.dbo.SYSREMOTE_TABLES <
  8305.                     @table_server,
  8306.                     @table_catalog_param,
  8307.                     NULL,
  8308.                     @table_name,
  8309.                     NULL >
  8310.         where (TABLE_SCHEMA like @table_schema
  8311.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8312.             and (TABLE_CATALOG like @table_catalog
  8313.             or     @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8314.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8315.             or     @table_type is NULL)
  8316.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8317.     end
  8318.     else
  8319.     begin    /*    If wild carding */
  8320.         select
  8321.             TABLE_CAT = TABLE_CATALOG,
  8322.             TABLE_SCHEM = TABLE_SCHEMA,
  8323.             TABLE_NAME = TABLE_NAME,
  8324.             TABLE_TYPE = TABLE_TYPE,
  8325.             REMARKS = convert(nvarchar(255),DESCRIPTION)
  8326.         from master.dbo.SYSREMOTE_TABLES <
  8327.                     @table_server,
  8328.                     @table_catalog_param,
  8329.                     NULL,
  8330.                     NULL,
  8331.                     NULL >
  8332.         where (TABLE_SCHEMA like @table_schema
  8333.             or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8334.             and (TABLE_CATALOG like @table_catalog
  8335.             or     @table_catalog is NULL or (TABLE_CATALOG is NULL and @table_catalog = N'%'))
  8336.             and (charindex ('''' + TABLE_TYPE + '''', @table_type) <> 0
  8337.             or     @table_type is NULL)
  8338.             and TABLE_NAME like @table_name
  8339.         order by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM, TABLE_NAME
  8340.     end
  8341. go
  8342. grant execute on sp_tables_ex to public
  8343. go
  8344.  
  8345.  
  8346. if object_id('sp_columns_ex', 'P') is not null
  8347.     drop proc sp_columns_ex
  8348. go
  8349.  
  8350. /* 7.0 version */
  8351. raiserror(15339,-1,-1,'sp_columns_ex')
  8352. go
  8353.  
  8354. create procedure sp_columns_ex(
  8355.     @table_server        sysname,
  8356.     @table_name            sysname = null,
  8357.     @table_schema        sysname = null,
  8358.     @table_catalog        sysname = null,
  8359.     @column_name        sysname = null,
  8360.     @ODBCVer            int = 2)
  8361. as
  8362.  
  8363.     set nocount on
  8364.     declare
  8365.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  8366.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  8367.  
  8368.     select
  8369.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  8370.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  8371.  
  8372.     declare    
  8373.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8374.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8375.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8376.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  8377.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8378.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  8379.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8380.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  8381.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  8382.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  8383.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  8384.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8385.     @DBTYPE_DBTIMESTAMP smallint
  8386.  
  8387.     select
  8388.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  8389.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  8390.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  8391.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8392.     @DBTYPE_VARNUMERIC    = 139,    @DBTYPE_DECIMAL        = 14,
  8393.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  8394.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  8395.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  8396.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  8397.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  8398.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  8399.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  8400.     @DBTYPE_DBTIMESTAMP    = 135
  8401.  
  8402.     declare    
  8403.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  8404.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  8405.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  8406.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8407.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8408.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  8409.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  8410.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  8411.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  8412.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  8413.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  8414.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  8415.  
  8416.     select     
  8417.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8418.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  8419.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8420.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8421.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8422.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8423.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  8424.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  8425.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  8426.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  8427.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  8428.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  8429.  
  8430.     declare 
  8431.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8432.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8433.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  8434.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  8435.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  8436.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8437.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  8438.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  8439.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  8440.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8441.     @ODBC_DATETIME smallint
  8442.  
  8443.     select
  8444.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  8445.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  8446.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  8447.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  8448.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  8449.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  8450.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  8451.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  8452.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  8453.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  8454.     @ODBC_DATETIME    =    
  8455.     case @ODBCVer
  8456.         when 2 then 11
  8457.         else 93
  8458.     end
  8459.  
  8460.     create table #tmp_columns
  8461.     (    TABLE_CAT sysname  NULL,
  8462.         TABLE_SCHEM sysname  NULL,
  8463.         TABLE_NAME sysname NOT NULL,
  8464.         COLUMN_NAME sysname  NULL,
  8465.         DATA_TYPE smallint NOT NULL,
  8466.         TYPE_NAME sysname   NULL,
  8467.         COLUMN_SIZE int NULL,
  8468.         BUFFER_LENGTH int NULL,
  8469.         DECIMAL_DIGITS smallint NULL,
  8470.         NUM_PREC_RADIX smallint NULL,
  8471.         NULLABLE smallint NOT NULL,
  8472.         REMARKS nvarchar(255)  NULL,
  8473.             COLUMN_DEF nvarchar(128)  NULL,
  8474.             SQL_DATA_TYPE smallint null,
  8475.             SQL_DATETIME_SUB smallint NULL,
  8476.         CHAR_OCTET_LENGTH int NULL,
  8477.         ORDINAL_POSITION smallint,
  8478.         IS_NULLABLE varchar(254)  NOT NULL,
  8479.         SS_DATA_TYPE tinyint null,
  8480.         COLUMN_FLAGS int NOT NULL)
  8481.  
  8482.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8483.         (isnull(charindex('[', @table_name),0) = 0) and
  8484.         (isnull(charindex('_', @table_name),0) = 0))
  8485.     begin    /*    If no wild carding */
  8486.         insert into #tmp_columns
  8487.         select
  8488.             TABLE_CAT = c.TABLE_CATALOG,
  8489.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8490.             TABLE_NAME = c.TABLE_NAME,
  8491.             COLUMN_NAME = c.COLUMN_NAME,
  8492.             DATA_TYPE =
  8493.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8494.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  8495.                 when @DBTYPE_I4                then @ODBC_INT
  8496.                 when @DBTYPE_R4                then @ODBC_REAL
  8497.                 when @DBTYPE_R8                then @ODBC_FLOAT
  8498.                 when @DBTYPE_CY                then @ODBC_MONEY
  8499.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  8500.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  8501.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  8502.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8503.                 when @DBTYPE_BOOL            then @ODBC_BIT
  8504.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8505.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8506.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  8507.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8508.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8509.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  8510.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8511.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8512.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  8513.                 when @DBTYPE_GUID            then @ODBC_GUID
  8514.                 when @DBTYPE_BYTES then
  8515.                     case
  8516.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8517.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8518.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8519.                                  p.COLUMN_SIZE = 8
  8520.                             then @ODBC_TIMESTAMP
  8521.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8522.                             then @ODBC_BINARY
  8523.                         else @ODBC_VARBINARY
  8524.                     end
  8525.                 when @DBTYPE_STR then
  8526.                     case
  8527.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8528.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8529.                         else @ODBC_VARCHAR
  8530.                     end
  8531.                 when @DBTYPE_WSTR then
  8532.                     case
  8533.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8534.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8535.                         else @ODBC_NVARCHAR
  8536.                     end
  8537.                 when @DBTYPE_BSTR then
  8538.                     case
  8539.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8540.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8541.                         else @ODBC_NVARCHAR
  8542.                     end
  8543.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  8544.             end,
  8545.             TYPE_NAME = p.TYPE_NAME,
  8546.             COLUMN_SIZE =
  8547.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8548.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8549.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8550.             NUM_PREC_RADIX = NULL,
  8551.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8552.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8553.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8554.             SQL_DATA_TYPE = NULL,
  8555.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8556.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8557.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8558.             IS_NULLABLE =
  8559.             case c.IS_NULLABLE
  8560.                 when 1 then 'YES'
  8561.                 else 'NO'
  8562.             end,
  8563.             SS_DATA_TYPE = NULL,
  8564.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8565.         from master.dbo.SYSREMOTE_COLUMNS <
  8566.                     @table_server,
  8567.                     @table_catalog,
  8568.                     NULL,
  8569.                     @table_name,
  8570.                     NULL > c,
  8571.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  8572.                     @table_server > p
  8573.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8574.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8575.                 (TABLE_SCHEMA like @table_schema
  8576.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8577.                 and (c.COLUMN_NAME like @column_name
  8578.                 or     @column_name is NULL)
  8579.     end
  8580.     else
  8581.     begin    /*    If wild carding */
  8582.         insert into #tmp_columns
  8583.         select
  8584.             TABLE_CAT = c.TABLE_CATALOG,
  8585.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8586.             TABLE_NAME = c.TABLE_NAME,
  8587.             COLUMN_NAME = c.COLUMN_NAME,
  8588.             DATA_TYPE =
  8589.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8590.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  8591.                 when @DBTYPE_I4                then @ODBC_INT
  8592.                 when @DBTYPE_R4                then @ODBC_REAL
  8593.                 when @DBTYPE_R8                then @ODBC_FLOAT
  8594.                 when @DBTYPE_CY                then @ODBC_MONEY
  8595.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  8596.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  8597.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  8598.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8599.                 when @DBTYPE_BOOL            then @ODBC_BIT
  8600.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8601.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8602.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  8603.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8604.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8605.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  8606.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8607.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8608.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  8609.                 when @DBTYPE_GUID            then @ODBC_GUID
  8610.                 when @DBTYPE_BYTES then
  8611.                     case
  8612.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8613.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8614.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8615.                                  p.COLUMN_SIZE = 8
  8616.                             then @ODBC_TIMESTAMP
  8617.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8618.                             then @ODBC_BINARY
  8619.                         else @ODBC_VARBINARY
  8620.                     end
  8621.                 when @DBTYPE_STR then
  8622.                     case
  8623.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8624.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8625.                         else @ODBC_VARCHAR
  8626.                     end
  8627.                 when @DBTYPE_WSTR then
  8628.                     case
  8629.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8630.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8631.                         else @ODBC_NVARCHAR
  8632.                     end
  8633.                 when @DBTYPE_BSTR then
  8634.                     case
  8635.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8636.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8637.                         else @ODBC_NVARCHAR
  8638.                     end
  8639.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  8640.             end,
  8641.             TYPE_NAME = p.TYPE_NAME,
  8642.             COLUMN_SIZE =
  8643.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  8644.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8645.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  8646.             NUM_PREC_RADIX = NULL,
  8647.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  8648.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  8649.             COLUMN_DEF = c.COLUMN_DEFAULT,
  8650.             SQL_DATA_TYPE = NULL,
  8651.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  8652.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  8653.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  8654.             IS_NULLABLE =
  8655.             case c.IS_NULLABLE
  8656.                 when 1 then 'YES'
  8657.                 else 'NO'
  8658.             end,
  8659.             SS_DATA_TYPE = NULL,
  8660.             COLUMN_FLAGS = c.COLUMN_FLAGS
  8661.         from master.dbo.SYSREMOTE_COLUMNS <
  8662.                     @table_server,
  8663.                     @table_catalog,
  8664.                     NULL,
  8665.                     NULL,
  8666.                     NULL > c,
  8667.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  8668.                     @table_server > p
  8669.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  8670.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  8671.                 (TABLE_SCHEMA like @table_schema
  8672.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  8673.                 and (c.COLUMN_NAME like @column_name
  8674.                 or     @column_name is NULL)
  8675.                 and c.TABLE_NAME like @table_name
  8676.     end
  8677.  
  8678.     update #tmp_columns
  8679.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  8680.         SQL_DATETIME_SUB =
  8681.         case
  8682.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  8683.             else #tmp_columns.SQL_DATETIME_SUB
  8684.         end,
  8685.         TYPE_NAME = 
  8686.         case 
  8687.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8688.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8689.                     BUFFER_LENGTH = 8
  8690.                 then 'timestamp'
  8691.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8692.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8693.                 then 'smalldatetime'
  8694.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  8695.                     #tmp_columns.COLUMN_SIZE = 10
  8696.                 then 'smallmoney'
  8697.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  8698.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  8699.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  8700.                 then spt_dt.TYPE_NAME
  8701.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME)
  8702.         end,
  8703.         COLUMN_SIZE =
  8704.         case
  8705.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  8706.                 spt_dt.data_precision
  8707.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  8708.                     #tmp_columns.SQL_DATETIME_SUB = 0
  8709.                 then 16
  8710.             else #tmp_columns.COLUMN_SIZE
  8711.         end,
  8712.         BUFFER_LENGTH =
  8713.         case
  8714.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  8715.                 COLUMN_SIZE+2
  8716.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  8717.             else #tmp_columns.BUFFER_LENGTH
  8718.         end,
  8719.         DECIMAL_DIGITS =
  8720.         case
  8721.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  8722.                 then #tmp_columns.SQL_DATETIME_SUB
  8723.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  8724.         end,
  8725.         NUM_PREC_RADIX = spt_dt.RADIX,
  8726.         SS_DATA_TYPE = 
  8727.         case
  8728.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  8729.                 and #tmp_columns.NULLABLE = 1
  8730.                     then @ODS_INT_NULL
  8731.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  8732.                 then @ODS_TINYINT_NOT_NULL
  8733.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  8734.                 then @ODS_SMALLINT_NOT_NULL
  8735.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  8736.                 then @ODS_INT_NOT_NULL
  8737.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  8738.                 then @ODS_FLOAT_NULL
  8739.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  8740.                 then @ODS_REAL_NOT_NULL
  8741.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  8742.                 then @ODS_FLOAT_NOT_NULL
  8743.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  8744.                 then @ODS_MONEY_NULL
  8745.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  8746.                 then @ODS_MONEY_NOT_NULL
  8747.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  8748.                 then @ODS_DATETIME_NULL
  8749.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  8750.                 then @ODS_DATETIME_NOT_NULL
  8751.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  8752.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  8753.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  8754.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  8755.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  8756.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  8757.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  8758.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8759.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8760.                     BUFFER_LENGTH = 8
  8761.                 then @ODS_TIMESTAMP
  8762.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  8763.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8764.                 then  @ODS_BINARY_NULL
  8765.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  8766.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8767.                 then @ODS_BINARY_NOT_NULL
  8768.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  8769.                 then @ODS_VARBINARY    
  8770.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  8771.                 then @ODS_CHAR_NULL
  8772.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  8773.                 then @ODS_CHAR_NOT_NULL
  8774.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  8775.             else null
  8776.         end
  8777.         from master.dbo.spt_datatype_info spt_dt
  8778.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  8779.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  8780.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  8781.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  8782.  
  8783.     select
  8784.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  8785.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  8786.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  8787.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  8788.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  8789.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  8790.             SS_DATA_TYPE
  8791.         from #tmp_columns
  8792.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  8793.  
  8794.         drop table #tmp_columns
  8795.  
  8796. go
  8797. if (charindex('8.00', @@version) > 0)
  8798.     drop procedure sp_columns_ex
  8799. else
  8800. begin
  8801.     print ''
  8802.     print ''
  8803.     print 'Warning:'
  8804.     print 'you are installing the stored procedures '
  8805.     print 'on a pre 8.0 SQL Server.'
  8806.     print 'Ignore the following errors.'
  8807. end
  8808. go
  8809.  
  8810. /* 8.0 version */
  8811. raiserror(15339,-1,-1,'sp_columns_ex')
  8812. go
  8813. create procedure sp_columns_ex(
  8814.     @table_server        sysname,
  8815.     @table_name            sysname = null,
  8816.     @table_schema        sysname = null,
  8817.     @table_catalog        sysname = null,
  8818.     @column_name        sysname = null,
  8819.     @ODBCVer            int = 2)
  8820. as
  8821.  
  8822.     set nocount on
  8823.     declare
  8824.     @DBCOLUMNFLAGS_ISFIXEDLENGTH binary(1),    @DBCOLUMNFLAGS_ISLONG    binary(1),
  8825.     @DBCOLUMNFLAGS_ISROWVER    binary(2)
  8826.  
  8827.     select
  8828.     @DBCOLUMNFLAGS_ISFIXEDLENGTH    = 0x10,    @DBCOLUMNFLAGS_ISLONG    = 0x80,
  8829.     @DBCOLUMNFLAGS_ISROWVER    = 0x0200
  8830.  
  8831.     declare    
  8832.     @DBTYPE_I2 smallint,            @DBTYPE_I4 smallint,
  8833.     @DBTYPE_R4 smallint,            @DBTYPE_R8 smallint,
  8834.     @DBTYPE_CY smallint,            @DBTYPE_DATE smallint,
  8835.     @DBTYPE_BSTR smallint,            @DBTYPE_BOOL smallint,
  8836.     @DBTYPE_VARNUMERIC smallint,    @DBTYPE_DECIMAL smallint,
  8837.     @DBTYPE_UI1 smallint,            @DBTYPE_BYREF smallint,
  8838.     @DBTYPE_I1 smallint,            @DBTYPE_UI2 smallint,
  8839.     @DBTYPE_UI4 smallint,            @DBTYPE_I8 smallint,
  8840.     @DBTYPE_UI8 smallint,            @DBTYPE_GUID smallint,
  8841.     @DBTYPE_BYTES smallint,            @DBTYPE_STR smallint,
  8842.     @DBTYPE_WSTR smallint,            @DBTYPE_NUMERIC smallint,
  8843.     @DBTYPE_DBDATE smallint,        @DBTYPE_DBTIME smallint,
  8844.     @DBTYPE_DBTIMESTAMP smallint
  8845.  
  8846.     select
  8847.     @DBTYPE_I2            = 2,    @DBTYPE_I4            = 3,
  8848.     @DBTYPE_R4            = 4,    @DBTYPE_R8            = 5,
  8849.     @DBTYPE_CY            = 6,    @DBTYPE_DATE        = 7,
  8850.     @DBTYPE_BSTR        = 8,    @DBTYPE_BOOL        = 11,
  8851.     @DBTYPE_VARNUMERIC    = 139,    @DBTYPE_DECIMAL        = 14,
  8852.     @DBTYPE_UI1            = 17,    @DBTYPE_BYREF        = 16384,
  8853.     @DBTYPE_I1            = 16,    @DBTYPE_UI2            = 18,
  8854.     @DBTYPE_UI4            = 19,    @DBTYPE_I8            = 20,
  8855.     @DBTYPE_UI8            = 21,    @DBTYPE_GUID        = 72,
  8856.     @DBTYPE_BYTES        = 128,    @DBTYPE_STR            = 129,
  8857.     @DBTYPE_WSTR        = 130,    @DBTYPE_NUMERIC        = 131,
  8858.     @DBTYPE_DBDATE        = 133,    @DBTYPE_DBTIME        = 134,    
  8859.     @DBTYPE_DBTIMESTAMP    = 135
  8860.  
  8861.     declare    
  8862.     @ODS_SMALLINT_NOT_NULL tinyint,    @ODS_INT_NULL tinyint,
  8863.     @ODS_INT_NOT_NULL tinyint,        @ODS_TINYINT_NOT_NULL tinyint,
  8864.     @ODS_REAL_NOT_NULL tinyint,        @ODS_FLOAT_NULL tinyint,
  8865.     @ODS_FLOAT_NOT_NULL tinyint,    @ODS_MONEY_NULL tinyint,
  8866.     @ODS_MONEY_NOT_NULL tinyint,    @ODS_DATETIME_NULL tinyint,
  8867.     @ODS_DATETIME_NOT_NULL tinyint,    @ODS_TEXT tinyint,
  8868.     @ODS_BIT tinyint,                @ODS_DECIMAL tinyint,
  8869.     @ODS_NUMERIC tinyint,            @ODS_GUID tinyint,
  8870.     @ODS_IMAGE tinyint,                @ODS_TIMESTAMP tinyint,
  8871.     @ODS_BINARY_NULL tinyint,        @ODS_BINARY_NOT_NULL tinyint,
  8872.     @ODS_VARBINARY tinyint,            @ODS_CHAR_NULL tinyint,
  8873.     @ODS_CHAR_NOT_NULL tinyint,        @ODS_VARCHAR tinyint
  8874.  
  8875.     select     
  8876.     @ODS_IMAGE = 34,            @ODS_TEXT = 35,
  8877.     @ODS_GUID = 36,                @ODS_BINARY_NULL = 37,            
  8878.     @ODS_VARBINARY = 37,        @ODS_INT_NULL = 38,
  8879.     @ODS_CHAR_NULL = 39,        @ODS_VARCHAR = 39,
  8880.     @ODS_TIMESTAMP = 45,        @ODS_BINARY_NOT_NULL = 45,
  8881.     @ODS_CHAR_NOT_NULL = 47,    @ODS_TINYINT_NOT_NULL = 48,
  8882.     @ODS_BIT = 50,                @ODS_SMALLINT_NOT_NULL = 52,    
  8883.     @ODS_INT_NOT_NULL = 56,        @ODS_REAL_NOT_NULL = 59,        
  8884.     @ODS_MONEY_NOT_NULL = 60,    @ODS_DATETIME_NOT_NULL = 61,    
  8885.     @ODS_FLOAT_NOT_NULL = 62,    @ODS_DECIMAL = 106,
  8886.     @ODS_NUMERIC = 108,            @ODS_FLOAT_NULL = 109,
  8887.     @ODS_MONEY_NULL = 110,        @ODS_DATETIME_NULL = 111
  8888.  
  8889.     declare 
  8890.     @ODBC_GUID smallint,        @ODBC_NTEXT smallint,
  8891.     @ODBC_NVARCHAR smallint,    @ODBC_NCHAR smallint,
  8892.     @ODBC_BIT smallint,            @ODBC_TINYINT smallint,
  8893.     @ODBC_IMAGE smallint,        @ODBC_VARBINARY smallint,
  8894.     @ODBC_TIMESTAMP smallint,    @ODBC_BINARY smallint,
  8895.     @ODBC_TEXT smallint,        @ODBC_CHAR smallint,
  8896.     @ODBC_NUMERIC smallint,        @ODBC_DECIMAL smallint,
  8897.     @ODBC_MONEY smallint,        @ODBC_INT smallint,        
  8898.     @ODBC_SMALLINT smallint,    @ODBC_FLOAT smallint,    
  8899.     @ODBC_REAL smallint,        @ODBC_VARCHAR smallint,
  8900.     @ODBC_DATETIME smallint
  8901.  
  8902.     select
  8903.     @ODBC_GUID        = -11,    @ODBC_NTEXT        = -10,
  8904.     @ODBC_NVARCHAR    = -9,    @ODBC_NCHAR        = -8,
  8905.     @ODBC_BIT        = -7,    @ODBC_TINYINT    = -6,
  8906.     @ODBC_IMAGE        = -4,    @ODBC_VARBINARY    = -3,
  8907.     @ODBC_TIMESTAMP    = -2,    @ODBC_BINARY    = -2,
  8908.     @ODBC_TEXT        = -1,    @ODBC_CHAR        = 1,
  8909.     @ODBC_NUMERIC    = 2,    @ODBC_DECIMAL    = 3,
  8910.     @ODBC_MONEY        = 3,    @ODBC_INT        = 4,        
  8911.     @ODBC_SMALLINT    = 5,    @ODBC_FLOAT        = 6,    
  8912.     @ODBC_REAL        = 7,    @ODBC_VARCHAR    = 12,
  8913.     @ODBC_DATETIME    =    
  8914.     case @ODBCVer
  8915.         when 2 then 11
  8916.         else 93
  8917.     end
  8918.  
  8919.     create table #tmp_columns
  8920.     (    TABLE_CAT sysname collate database_default NULL,
  8921.         TABLE_SCHEM sysname collate database_default NULL,
  8922.         TABLE_NAME sysname    collate database_default NOT NULL,
  8923.         COLUMN_NAME sysname collate database_default NULL,
  8924.         DATA_TYPE smallint NOT NULL,
  8925.         TYPE_NAME sysname  collate database_default NULL,
  8926.         COLUMN_SIZE int NULL,
  8927.         BUFFER_LENGTH int NULL,
  8928.         DECIMAL_DIGITS smallint NULL,
  8929.         NUM_PREC_RADIX smallint NULL,
  8930.         NULLABLE smallint NOT NULL,
  8931.         REMARKS nvarchar(255) collate database_default NULL,
  8932.             COLUMN_DEF nvarchar(128) collate database_default NULL,
  8933.             SQL_DATA_TYPE smallint null,
  8934.             SQL_DATETIME_SUB smallint NULL,
  8935.         CHAR_OCTET_LENGTH int NULL,
  8936.         ORDINAL_POSITION smallint,
  8937.         IS_NULLABLE varchar(254) collate database_default NOT NULL,
  8938.         SS_DATA_TYPE tinyint null,
  8939.         COLUMN_FLAGS int NOT NULL)
  8940.  
  8941.     if ((isnull(charindex('%', @table_name),0) = 0) and
  8942.         (isnull(charindex('[', @table_name),0) = 0) and
  8943.         (isnull(charindex('_', @table_name),0) = 0))
  8944.     begin    /*    If no wild carding */
  8945.         insert into #tmp_columns
  8946.         select
  8947.             TABLE_CAT = c.TABLE_CATALOG,
  8948.             TABLE_SCHEM = c.TABLE_SCHEMA,
  8949.             TABLE_NAME = c.TABLE_NAME,
  8950.             COLUMN_NAME = c.COLUMN_NAME,
  8951.             DATA_TYPE =
  8952.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  8953.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  8954.                 when @DBTYPE_I4                then @ODBC_INT
  8955.                 when @DBTYPE_R4                then @ODBC_REAL
  8956.                 when @DBTYPE_R8                then @ODBC_FLOAT
  8957.                 when @DBTYPE_CY                then @ODBC_MONEY
  8958.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  8959.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  8960.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  8961.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  8962.                 when @DBTYPE_BOOL            then @ODBC_BIT
  8963.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  8964.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  8965.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  8966.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  8967.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  8968.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  8969.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  8970.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  8971.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  8972.                 when @DBTYPE_GUID            then @ODBC_GUID
  8973.                 when @DBTYPE_BYTES then
  8974.                     case
  8975.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  8976.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  8977.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  8978.                                  p.COLUMN_SIZE = 8
  8979.                             then @ODBC_TIMESTAMP
  8980.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  8981.                             then @ODBC_BINARY
  8982.                         else @ODBC_VARBINARY
  8983.                     end
  8984.                 when @DBTYPE_STR then
  8985.                     case
  8986.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  8987.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  8988.                         else @ODBC_VARCHAR
  8989.                     end
  8990.                 when @DBTYPE_WSTR then
  8991.                     case
  8992.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8993.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  8994.                         else @ODBC_NVARCHAR
  8995.                     end
  8996.                 when @DBTYPE_BSTR then
  8997.                     case
  8998.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  8999.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9000.                         else @ODBC_NVARCHAR
  9001.                     end
  9002.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  9003.             end,
  9004.             TYPE_NAME = p.TYPE_NAME,
  9005.             COLUMN_SIZE =
  9006.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9007.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9008.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9009.             NUM_PREC_RADIX = NULL,
  9010.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9011.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9012.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9013.             SQL_DATA_TYPE = NULL,
  9014.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9015.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9016.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9017.             IS_NULLABLE =
  9018.             case c.IS_NULLABLE
  9019.                 when 1 then 'YES'
  9020.                 else 'NO'
  9021.             end,
  9022.             SS_DATA_TYPE = NULL,
  9023.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9024.         from master.dbo.SYSREMOTE_COLUMNS <
  9025.                     @table_server,
  9026.                     @table_catalog,
  9027.                     NULL,
  9028.                     @table_name,
  9029.                     NULL > c,
  9030.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  9031.                     @table_server > p
  9032.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9033.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9034.                 (TABLE_SCHEMA like @table_schema
  9035.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9036.                 and (c.COLUMN_NAME like @column_name
  9037.                 or     @column_name is NULL)
  9038.     end
  9039.     else
  9040.     begin    /*    If wild carding */
  9041.         insert into #tmp_columns
  9042.         select
  9043.             TABLE_CAT = c.TABLE_CATALOG,
  9044.             TABLE_SCHEM = c.TABLE_SCHEMA,
  9045.             TABLE_NAME = c.TABLE_NAME,
  9046.             COLUMN_NAME = c.COLUMN_NAME,
  9047.             DATA_TYPE =
  9048.             case c.DATA_TYPE & ~@DBTYPE_BYREF
  9049.                 when @DBTYPE_I2                then @ODBC_SMALLINT
  9050.                 when @DBTYPE_I4                then @ODBC_INT
  9051.                 when @DBTYPE_R4                then @ODBC_REAL
  9052.                 when @DBTYPE_R8                then @ODBC_FLOAT
  9053.                 when @DBTYPE_CY                then @ODBC_MONEY
  9054.                 when @DBTYPE_DATE            then @ODBC_DATETIME
  9055.                 when @DBTYPE_DBDATE            then @ODBC_DATETIME
  9056.                 when @DBTYPE_DBTIME            then @ODBC_DATETIME
  9057.                 when @DBTYPE_DBTIMESTAMP    then @ODBC_DATETIME
  9058.                 when @DBTYPE_BOOL            then @ODBC_BIT
  9059.                 when @DBTYPE_DECIMAL        then @ODBC_DECIMAL
  9060.                 when @DBTYPE_UI1            then @ODBC_TINYINT
  9061.                 when @DBTYPE_I1                then @ODBC_NUMERIC
  9062.                 when @DBTYPE_UI2            then @ODBC_NUMERIC
  9063.                 when @DBTYPE_UI4            then @ODBC_NUMERIC
  9064.                 when @DBTYPE_I8                then @ODBC_NUMERIC
  9065.                 when @DBTYPE_UI8            then @ODBC_NUMERIC
  9066.                 when @DBTYPE_NUMERIC        then @ODBC_NUMERIC
  9067.                 when @DBTYPE_VARNUMERIC        then @ODBC_NUMERIC
  9068.                 when @DBTYPE_GUID            then @ODBC_GUID
  9069.                 when @DBTYPE_BYTES then
  9070.                     case
  9071.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_IMAGE
  9072.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9073.                                 (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9074.                                  p.COLUMN_SIZE = 8
  9075.                             then @ODBC_TIMESTAMP
  9076.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9077.                             then @ODBC_BINARY
  9078.                         else @ODBC_VARBINARY
  9079.                     end
  9080.                 when @DBTYPE_STR then
  9081.                     case
  9082.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG ) or (p.COLUMN_SIZE > 8000) then @ODBC_TEXT
  9083.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_CHAR
  9084.                         else @ODBC_VARCHAR
  9085.                     end
  9086.                 when @DBTYPE_WSTR then
  9087.                     case
  9088.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9089.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9090.                         else @ODBC_NVARCHAR
  9091.                     end
  9092.                 when @DBTYPE_BSTR then
  9093.                     case
  9094.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISLONG = @DBCOLUMNFLAGS_ISLONG) or (p.COLUMN_SIZE > 4000) then @ODBC_NTEXT
  9095.                         when (c.COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH) then @ODBC_NCHAR
  9096.                         else @ODBC_NVARCHAR
  9097.                     end
  9098.                 else @ODBC_NVARCHAR     /*    Unknown OleDB datatype */
  9099.             end,
  9100.             TYPE_NAME = p.TYPE_NAME,
  9101.             COLUMN_SIZE =
  9102.                 COALESCE(c.CHARACTER_MAXIMUM_LENGTH, c.NUMERIC_PRECISION, p.COLUMN_SIZE),
  9103.             BUFFER_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9104.             DECIMAL_DIGITS = c.NUMERIC_SCALE,
  9105.             NUM_PREC_RADIX = NULL,
  9106.             NULLABLE = convert(smallint, c.IS_NULLABLE),
  9107.             REMARKS = convert(nvarchar(255),c.DESCRIPTION),
  9108.             COLUMN_DEF = c.COLUMN_DEFAULT,
  9109.             SQL_DATA_TYPE = NULL,
  9110.             SQL_DATETIME_SUB = convert(smallint, c.DATETIME_PRECISION),
  9111.             CHAR_OCTET_LENGTH = c.CHARACTER_OCTET_LENGTH,
  9112.             ORDINAL_POSITION = c.ORDINAL_POSITION,
  9113.             IS_NULLABLE =
  9114.             case c.IS_NULLABLE
  9115.                 when 1 then 'YES'
  9116.                 else 'NO'
  9117.             end,
  9118.             SS_DATA_TYPE = NULL,
  9119.             COLUMN_FLAGS = c.COLUMN_FLAGS
  9120.         from master.dbo.SYSREMOTE_COLUMNS <
  9121.                     @table_server,
  9122.                     @table_catalog,
  9123.                     NULL,
  9124.                     NULL,
  9125.                     NULL > c,
  9126.                 master.dbo.SYSREMOTE_PROVIDER_TYPES <
  9127.                     @table_server > p
  9128.                     /* LUXOR/KAGERA don't support restricting DATA_TYPE */
  9129.             where c.DATA_TYPE = p.DATA_TYPE AND p.BEST_MATCH = 1 and
  9130.                 (TABLE_SCHEMA like @table_schema
  9131.                 or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9132.                 and (c.COLUMN_NAME like @column_name
  9133.                 or     @column_name is NULL)
  9134.                 and c.TABLE_NAME like @table_name
  9135.     end
  9136.  
  9137.     update #tmp_columns
  9138.     set SQL_DATA_TYPE = spt_dt.SQL_DATA_TYPE,
  9139.         SQL_DATETIME_SUB =
  9140.         case
  9141.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME then 3
  9142.             else #tmp_columns.SQL_DATETIME_SUB
  9143.         end,
  9144.         TYPE_NAME = 
  9145.         case 
  9146.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9147.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9148.                     BUFFER_LENGTH = 8
  9149.                 then 'timestamp'
  9150.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9151.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9152.                 then 'smalldatetime'
  9153.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and
  9154.                     #tmp_columns.COLUMN_SIZE = 10
  9155.                 then 'smallmoney'
  9156.             when (#tmp_columns.DATA_TYPE in (@ODBC_BINARY, @ODBC_CHAR) and
  9157.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH) or
  9158.                  #tmp_columns.DATA_TYPE in (@ODBC_TEXT, @ODBC_NTEXT, @ODBC_IMAGE)
  9159.                 then spt_dt.TYPE_NAME collate database_default
  9160.             else isnull(#tmp_columns.TYPE_NAME, spt_dt.TYPE_NAME collate database_default)
  9161.         end,
  9162.         COLUMN_SIZE =
  9163.         case
  9164.             when #tmp_columns.DATA_TYPE IN (@ODBC_REAL, @ODBC_FLOAT) then /* app. numeric types */
  9165.                 spt_dt.data_precision
  9166.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and
  9167.                     #tmp_columns.SQL_DATETIME_SUB = 0
  9168.                 then 16
  9169.             else #tmp_columns.COLUMN_SIZE
  9170.         end,
  9171.         BUFFER_LENGTH =
  9172.         case
  9173.             when #tmp_columns.DATA_TYPE IN (@ODBC_NUMERIC, @ODBC_DECIMAL) then    /* decimal/numeric types */
  9174.                 COLUMN_SIZE+2
  9175.             when isnull (#tmp_columns.BUFFER_LENGTH,0) = 0 then spt_dt.length
  9176.             else #tmp_columns.BUFFER_LENGTH
  9177.         end,
  9178.         DECIMAL_DIGITS =
  9179.         case
  9180.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME
  9181.                 then #tmp_columns.SQL_DATETIME_SUB
  9182.             else COALESCE (DECIMAL_DIGITS, spt_dt.numeric_scale)
  9183.         end,
  9184.         NUM_PREC_RADIX = spt_dt.RADIX,
  9185.         SS_DATA_TYPE = 
  9186.         case
  9187.             when #tmp_columns.DATA_TYPE in ( @ODBC_TINYINT, @ODBC_SMALLINT, @ODBC_INT ) 
  9188.                 and #tmp_columns.NULLABLE = 1
  9189.                     then @ODS_INT_NULL
  9190.             when #tmp_columns.DATA_TYPE = @ODBC_TINYINT and #tmp_columns.NULLABLE = 0 
  9191.                 then @ODS_TINYINT_NOT_NULL
  9192.             when #tmp_columns.DATA_TYPE = @ODBC_SMALLINT    and #tmp_columns.NULLABLE = 0
  9193.                 then @ODS_SMALLINT_NOT_NULL
  9194.             when #tmp_columns.DATA_TYPE = @ODBC_INT  and #tmp_columns.NULLABLE = 0
  9195.                 then @ODS_INT_NOT_NULL
  9196.             when #tmp_columns.DATA_TYPE in (@ODBC_REAL, @ODBC_FLOAT) and #tmp_columns.NULLABLE = 1
  9197.                 then @ODS_FLOAT_NULL
  9198.             when #tmp_columns.DATA_TYPE = @ODBC_REAL and #tmp_columns.NULLABLE = 0
  9199.                 then @ODS_REAL_NOT_NULL
  9200.             when #tmp_columns.DATA_TYPE = @ODBC_FLOAT and #tmp_columns.NULLABLE = 0
  9201.                 then @ODS_FLOAT_NOT_NULL
  9202.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 1 
  9203.                 then @ODS_MONEY_NULL
  9204.             when #tmp_columns.DATA_TYPE = @ODBC_MONEY and #tmp_columns.NULLABLE = 0 
  9205.                 then @ODS_MONEY_NOT_NULL
  9206.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 1
  9207.                 then @ODS_DATETIME_NULL
  9208.             when #tmp_columns.DATA_TYPE = @ODBC_DATETIME and #tmp_columns.NULLABLE = 0
  9209.                 then @ODS_DATETIME_NOT_NULL
  9210.             when #tmp_columns.DATA_TYPE in (@ODBC_NTEXT, @ODBC_TEXT) then @ODS_TEXT
  9211.             when #tmp_columns.DATA_TYPE = @ODBC_BIT then @ODS_BIT
  9212.             when #tmp_columns.DATA_TYPE = @ODBC_DECIMAL then  @ODS_DECIMAL    
  9213.             when #tmp_columns.DATA_TYPE = @ODBC_NUMERIC then  @ODS_NUMERIC
  9214.             when #tmp_columns.DATA_TYPE = @ODBC_GUID then @ODS_GUID
  9215.             when #tmp_columns.DATA_TYPE = @ODBC_IMAGE then  @ODS_IMAGE
  9216.             when #tmp_columns.DATA_TYPE = @ODBC_TIMESTAMP
  9217.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISROWVER) = @DBCOLUMNFLAGS_ISROWVER AND
  9218.                     (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH) = @DBCOLUMNFLAGS_ISFIXEDLENGTH and
  9219.                     BUFFER_LENGTH = 8
  9220.                 then @ODS_TIMESTAMP
  9221.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    1
  9222.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9223.                 then  @ODS_BINARY_NULL
  9224.             when #tmp_columns.DATA_TYPE = @ODBC_BINARY and #tmp_columns.NULLABLE =    0
  9225.                 and (COLUMN_FLAGS & @DBCOLUMNFLAGS_ISFIXEDLENGTH = @DBCOLUMNFLAGS_ISFIXEDLENGTH)
  9226.                 then @ODS_BINARY_NOT_NULL
  9227.             when #tmp_columns.DATA_TYPE = @ODBC_VARBINARY
  9228.                 then @ODS_VARBINARY    
  9229.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 1
  9230.                 then @ODS_CHAR_NULL
  9231.             when #tmp_columns.DATA_TYPE in (@ODBC_CHAR, @ODBC_NCHAR) and #tmp_columns.NULLABLE = 0
  9232.                 then @ODS_CHAR_NOT_NULL
  9233.             when #tmp_columns.DATA_TYPE in (@ODBC_VARCHAR, @ODBC_NVARCHAR) then @ODS_VARCHAR
  9234.             else null
  9235.         end
  9236.         from master.dbo.spt_datatype_info spt_dt
  9237.         where spt_dt.DATA_TYPE = #tmp_columns.DATA_TYPE
  9238.             AND (spt_dt.ODBCVer is null or spt_dt.ODBCVer = @ODBCVer)
  9239.             and isnull(spt_dt.AUTO_INCREMENT,0) = 0
  9240.             and not spt_dt.TYPE_NAME = 'smalldatetime'
  9241.  
  9242.     select
  9243.             TABLE_CAT,            TABLE_SCHEM,        TABLE_NAME,
  9244.             COLUMN_NAME,        DATA_TYPE,            TYPE_NAME,
  9245.             COLUMN_SIZE,        BUFFER_LENGTH,        DECIMAL_DIGITS,
  9246.             NUM_PREC_RADIX,        NULLABLE,            REMARKS,
  9247.             COLUMN_DEF,            SQL_DATA_TYPE,        SQL_DATETIME_SUB,
  9248.             CHAR_OCTET_LENGTH,    ORDINAL_POSITION,    IS_NULLABLE,
  9249.             SS_DATA_TYPE
  9250.         from #tmp_columns
  9251.         order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
  9252.  
  9253.         drop table #tmp_columns
  9254.  
  9255. go
  9256. grant execute on sp_columns_ex to public
  9257. go
  9258.  
  9259.  
  9260. if object_id('sp_table_privileges_ex', 'P') is not null
  9261.     drop proc sp_table_privileges_ex
  9262. go
  9263. raiserror(15339,-1,-1,'sp_table_privileges_ex')
  9264. go
  9265. create procedure sp_table_privileges_ex(
  9266.     @table_server        sysname,
  9267.     @table_name            sysname = null,
  9268.     @table_schema        sysname = null,
  9269.     @table_catalog        sysname = null)
  9270. as
  9271.     select
  9272.         TABLE_CAT = TABLE_CATALOG,
  9273.         TABLE_SCHEM = TABLE_SCHEMA,
  9274.         TABLE_NAME = TABLE_NAME,
  9275.         GRANTOR = GRANTOR,
  9276.         GRANTEE = GRANTEE,
  9277.         PRIVILEGE = PRIVILEGE_TYPE,
  9278.         IS_GRANTABLE = 
  9279.         case IS_GRANTABLE
  9280.             when 1 then 'YES'
  9281.             when 0 then 'NO'
  9282.             else null
  9283.         end
  9284.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  9285.                 @table_server,
  9286.                 @table_catalog,
  9287.                 NULL,
  9288.                 NULL >
  9289.     where (TABLE_SCHEMA like @table_schema
  9290.         or @table_schema is NULL or (TABLE_SCHEMA is NULL and @table_schema = N'%'))
  9291.         and (TABLE_NAME like @table_name
  9292.         or     @table_name is NULL)
  9293.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, PRIVILEGE
  9294. go
  9295. grant execute on sp_table_privileges_ex to public
  9296. go
  9297.  
  9298.  
  9299. if object_id('sp_column_privileges_ex', 'P') is not null
  9300.     drop proc sp_column_privileges_ex
  9301. go
  9302. raiserror(15339,-1,-1,'sp_column_privileges_ex')
  9303. go
  9304. create procedure sp_column_privileges_ex(
  9305.     @table_server        sysname,
  9306.     @table_name            sysname = null,
  9307.     @table_schema        sysname = null,
  9308.     @table_catalog        sysname = null,
  9309.     @column_name        sysname = null)
  9310. as
  9311.     select
  9312.         TABLE_CAT = TABLE_CATALOG,
  9313.         TABLE_SCHEM = TABLE_SCHEMA,
  9314.         TABLE_NAME = TABLE_NAME,
  9315.         COLUMN_NAME = COLUMN_NAME,
  9316.         GRANTOR = GRANTOR,
  9317.         GRANTEE = GRANTEE,
  9318.         PRIVILEGE = PRIVILEGE_TYPE,
  9319.         IS_GRANTABLE = 
  9320.         case IS_GRANTABLE
  9321.             when 1 then 'YES'
  9322.             when 0 then 'NO'
  9323.             else null
  9324.         end
  9325.     from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  9326.         @table_server,
  9327.         @table_catalog,
  9328.         @table_schema,
  9329.         @table_name,
  9330.         NULL >
  9331.     where (COLUMN_NAME like @column_name
  9332.         or @column_name is NULL)
  9333.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, PRIVILEGE
  9334. go
  9335. grant execute on sp_column_privileges_ex to public
  9336. go
  9337.  
  9338.  
  9339. if object_id('sp_indexes', 'P') is not null
  9340.     drop proc sp_indexes
  9341. go
  9342. raiserror(15339,-1,-1,'sp_indexes')
  9343. go
  9344. create procedure sp_indexes(
  9345.     @table_server        sysname,
  9346.     @table_name            sysname = null,
  9347.     @table_schema        sysname = null,
  9348.     @table_catalog        sysname = null,
  9349.     @index_name            sysname = null,
  9350.     @is_unique          bit = null)
  9351. as
  9352.     select
  9353.         TABLE_CAT = TABLE_CATALOG,
  9354.         TABLE_SCHEM = TABLE_SCHEMA,
  9355.         TABLE_NAME = TABLE_NAME,
  9356.             NON_UNIQUE = convert(smallint, 1 - [UNIQUE]),
  9357.         INDEX_QUALIFIER = TABLE_NAME,
  9358.         INDEX_NAME = INDEX_NAME,
  9359.             TYPE =
  9360.         case [CLUSTERED]
  9361.             when 1 then 1
  9362.             else 3
  9363.         end,
  9364.         ORDINAL_POSITION = ORDINAL_POSITION,
  9365.             COLUMN_NAME = COLUMN_NAME,
  9366.         ASC_OR_DESC = 
  9367.         case [COLLATION]
  9368.             when 1 then 'A'
  9369.             when 2 then 'D'
  9370.             else null
  9371.         end,
  9372.             CARDINALITY = CARDINALITY,
  9373.         PAGES = 
  9374.         case [CLUSTERED]
  9375.             when 1 then PAGES
  9376.             else NULL
  9377.         end,
  9378.             FILTER_CONDITION = FILTER_CONDITION
  9379.     from master.dbo.SYSREMOTE_INDEXES <
  9380.                 @table_server,
  9381.                 @table_catalog,
  9382.                 @table_schema,
  9383.                         @index_name,
  9384.                 NULL,            /* TYPE (index type) */
  9385.                 @table_name >
  9386.     where @is_unique is null or @is_unique = [UNIQUE]
  9387.     order by NON_UNIQUE, TYPE, INDEX_QUALIFIER, INDEX_NAME, ORDINAL_POSITION
  9388. go
  9389. grant execute on sp_indexes to public
  9390. go
  9391.  
  9392.  
  9393. if object_id('sp_foreignkeys', 'P') is not null
  9394.     drop proc sp_foreignkeys
  9395. go
  9396. raiserror(15339,-1,-1,'sp_foreignkeys')
  9397. go
  9398. create procedure sp_foreignkeys(
  9399.     @table_server        sysname,
  9400.     @pktab_name         sysname = null,
  9401.     @pktab_schema       sysname = null,
  9402.     @pktab_catalog      sysname = null,
  9403.     @fktab_name         sysname = null,
  9404.     @fktab_schema       sysname = null,
  9405.     @fktab_catalog      sysname = null)
  9406. as
  9407.     select
  9408.         PKTABLE_CAT = PK_TABLE_CATALOG,
  9409.         PKTABLE_SCHEM = PK_TABLE_SCHEMA,
  9410.         PKTABLE_NAME = PK_TABLE_NAME,
  9411.         PKCOLUMN_NAME = PK_COLUMN_NAME,
  9412.         FKTABLE_CAT = FK_TABLE_CATALOG,
  9413.         FKTABLE_SCHEM = FK_TABLE_SCHEMA,
  9414.         FKTABLE_NAME = FK_TABLE_NAME,
  9415.         FKCOLUMN_NAME = FK_COLUMN_NAME,
  9416.         KEY_SEQ = ORDINAL,
  9417.         UPDATE_RULE = 
  9418.         case UPDATE_RULE
  9419.             when 'CASCADE' then 0
  9420.             when 'NO ACTION' then 1
  9421.             when 'SET NULL' then 2
  9422.             else null
  9423.         end,
  9424.         DELETE_RULE = 
  9425.         case DELETE_RULE
  9426.             when 'CASCADE' then 0
  9427.             when 'NO ACTION' then 1
  9428.             when 'SET NULL' then 2
  9429.             else null
  9430.         end,
  9431.         FK_NAME = convert(sysname, NULL),
  9432.         PK_NAME = convert(sysname, NULL),
  9433.         DEFERRABILITY = convert(smallint, null)
  9434.  
  9435.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  9436.                 @table_server,
  9437.                 @pktab_catalog,
  9438.                 @pktab_schema,
  9439.                 @pktab_name,
  9440.                 @fktab_catalog,
  9441.                 @fktab_schema,
  9442.                 @fktab_name >
  9443.     order by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, KEY_SEQ
  9444. go
  9445. grant execute on sp_foreignkeys to public
  9446. go
  9447.  
  9448.  
  9449. if object_id('sp_primarykeys', 'P') is not null
  9450.     drop proc sp_primarykeys
  9451. go
  9452. raiserror(15339,-1,-1,'sp_primarykeys')
  9453. go
  9454. create procedure sp_primarykeys(
  9455.     @table_server        sysname,
  9456.     @table_name         sysname = null,
  9457.     @table_schema       sysname = null,
  9458.     @table_catalog      sysname = null)
  9459. as
  9460.     select
  9461.         TABLE_CAT = TABLE_CATALOG,
  9462.         TABLE_SCHEM = TABLE_SCHEMA,
  9463.         TABLE_NAME = TABLE_NAME,
  9464.         COLUMN_NAME = COLUMN_NAME,
  9465.         KEY_SEQ = ORDINAL,
  9466.         PK_NAME = convert(sysname, NULL)
  9467.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  9468.                 @table_server,
  9469.                 @table_catalog,
  9470.                 @table_schema,
  9471.                 @table_name >
  9472.     order by TABLE_CAT, TABLE_SCHEM, TABLE_NAME, KEY_SEQ
  9473. go
  9474. grant execute on sp_primarykeys to public
  9475. go
  9476.  
  9477. /*-------------- END CATALOG STORED PROCEDURES FOR OLEDB SOURCES ------------------*/
  9478.  
  9479.  
  9480. dump tran master with no_log
  9481. go
  9482.  
  9483. if (charindex('6.50', @@version) = 0 and
  9484.     charindex('7.00', @@version) = 0 and
  9485.     charindex('8.00', @@version) = 0)
  9486. begin
  9487.     print ''
  9488.     print ''
  9489.     print 'Warning:'
  9490.     print 'you are installing the stored procedures '
  9491.     print 'on a pre 6.50 SQL Server.'
  9492.     print 'Ignore the following errors.'
  9493. end
  9494.  
  9495. print 'creating sp_ddopen'
  9496. go
  9497.  
  9498. /*    Procedure for pre-6.50 server */
  9499. create procedure sp_ddopen(
  9500.                @handle            int output,
  9501.                @procname        sysname,
  9502.                @scrollopt        int output,
  9503.                @ccopt            int output,
  9504.                @rows            int output,
  9505.                @p1                varchar(255) = null,
  9506.                @p2                varchar(255) = null,
  9507.                @p3                varchar(255) = null,
  9508.                @p4                varchar(255) = null,
  9509.                @p5                varchar(255) = null,
  9510.                @p6                varchar(255) = null,
  9511.                @p7                int = null,
  9512.                @ODBCVer         int = 2)
  9513. as
  9514.     set nocount on
  9515.     declare @ret int
  9516.  
  9517.     if @procname = 'sp_column_privileges'
  9518.     begin
  9519.         create table #spcolpriv (
  9520.             TABLE_QUALIFIER varchar(32) null,
  9521.             TABLE_OWNER varchar(32) null,
  9522.             TABLE_NAME varchar(32)    not null,
  9523.             COLUMN_NAME varchar(32) not null,
  9524.             GRANTOR varchar(32) null,
  9525.             GRANTEE varchar(32) not null,
  9526.             PRIVILEGE varchar(32) not null,
  9527.             IS_GRANTABLE varchar(3) null
  9528.             )
  9529.         insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  9530.         exec @ret = sp_cursoropen @handle output,
  9531.             'select * from #spcolpriv',
  9532.             @scrollopt output, @ccopt output, @rows output
  9533.         drop table #spcolpriv
  9534.     end
  9535.     else if @procname = 'sp_columns'
  9536.     begin
  9537.         create table #spcolumns (
  9538.             TABLE_QUALIFIER varchar(32) null,
  9539.             TABLE_OWNER varchar(32) null,
  9540.             TABLE_NAME varchar(32)    not null,
  9541.             COLUMN_NAME varchar(32) not null,
  9542.             DATA_TYPE smallint not null,
  9543.             TYPE_NAME varchar(32) not null,
  9544.             "PRECISION" int null,
  9545.             LENGTH int null,
  9546.             SCALE smallint null,
  9547.             RADIX smallint null,
  9548.             NULLABLE smallint not null,
  9549.             REMARKS varchar(254) null,
  9550.             COLUMN_DEF varchar(255) null,
  9551.             SQL_DATA_TYPE smallint not null,
  9552.             SQL_DATETIME_SUB smallint null,
  9553.             CHAR_OCTET_LENGTH int null,
  9554.             ORDINAL_POSITION int not null,
  9555.             IS_NULLABLE varchar(254) null,
  9556.             SS_DATA_TYPE tinyint null
  9557.             )
  9558.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  9559.         exec @ret = sp_cursoropen @handle output,
  9560.             'select * from #spcolumns',
  9561.             @scrollopt output, @ccopt output, @rows output
  9562.         drop table #spcolumns
  9563.     end
  9564.     else if @procname = 'sp_datatype_info'
  9565.     begin
  9566.         create table #spdatatypeinfo (
  9567.             TYPE_NAME            varchar(32)  not null,
  9568.             DATA_TYPE            smallint not null,
  9569.             "PRECISION"            int null,
  9570.             LITERAL_PREFIX        varchar(32)    null,
  9571.             LITERAL_SUFFIX        varchar(32)    null,
  9572.             CREATE_PARAMS        varchar(32)    null,
  9573.             NULLABLE            smallint   not null,
  9574.             CASE_SENSITIVE        smallint   not null,
  9575.             SEARCHABLE            smallint   not null,
  9576.             UNSIGNED_ATTRIBUTE    smallint   null,
  9577.             MONEY    smallint    not null,
  9578.             AUTO_INCREMENT        smallint    null,
  9579.             LOCAL_TYPE_NAME     varchar(32) null,
  9580.             MINIMUM_SCALE        smallint     null,
  9581.             MAXIMUM_SCALE        smallint   null,
  9582.             SQL_DATA_TYPE        smallint      not null,
  9583.             SQL_DATETIME_SUB    smallint   null,
  9584.             NUM_PREC_RADIX        int     null,
  9585.             INTERVAL_PRECISION    smallint    NULL,
  9586.             USERTYPE            smallint not null)
  9587.         insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  9588.         exec @ret = sp_cursoropen @handle output,
  9589.             'select * from #spdatatypeinfo',
  9590.             @scrollopt output, @ccopt output, @rows output
  9591.         drop table #spdatatypeinfo
  9592.     end
  9593.     else if @procname = 'sp_fkeys'
  9594.     begin
  9595.         create table #spfkeys (
  9596.             PKTABLE_QUALIFIER varchar(32)     null,
  9597.             PKTABLE_OWNER varchar(32)    null,
  9598.             PKTABLE_NAME varchar(32)  not null,
  9599.             PKCOLUMN_NAME varchar(32)    not null,
  9600.             FKTABLE_QUALIFIER varchar(32)    null,
  9601.             FKTABLE_OWNER varchar(32)    null,
  9602.             FKTABLE_NAME varchar(32)  not null,
  9603.             FKCOLUMN_NAME varchar(32)    not null,
  9604.             KEY_SEQ smallint not null,
  9605.             UPDATE_RULE smallint null,
  9606.             DELETE_RULE smallint null,
  9607.             FK_NAME varchar(32) null,
  9608.             PK_NAME varchar(32) null,
  9609.             DEFERRABILITY smallint null
  9610.             )
  9611.         insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  9612.         exec @ret = sp_cursoropen @handle output,
  9613.             'select * from #spfkeys',
  9614.             @scrollopt output, @ccopt output, @rows output
  9615.         drop table #spfkeys
  9616.     end
  9617.     else if @procname = 'sp_pkeys'
  9618.     begin
  9619.         create table #sppkeys (
  9620.             TABLE_QUALIFIER varchar(32)   null,
  9621.             TABLE_OWNER varchar(32)   null,
  9622.             TABLE_NAME varchar(32)    not null,
  9623.             COLUMN_NAME varchar(32)  not null,
  9624.             KEY_SEQ smallint not null,
  9625.             PK_NAME varchar(32) null
  9626.             )
  9627.         insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  9628.         exec @ret = sp_cursoropen @handle output,
  9629.             'select * from #sppkeys',
  9630.             @scrollopt output, @ccopt output, @rows output
  9631.         drop table #sppkeys
  9632.     end
  9633.     else if @procname = 'sp_special_columns'
  9634.     begin
  9635.         create table #spspeccol (
  9636.             SCOPE smallint null,
  9637.             COLUMN_NAME varchar(32) not null,
  9638.             DATA_TYPE smallint not null,
  9639.             TYPE_NAME varchar(32) not null,
  9640.             "PRECISION" int null,
  9641.             LENGTH int null,
  9642.             SCALE smallint null,
  9643.             PSEUDO_COLUMN smallint null
  9644.             )
  9645.         insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  9646.         exec @ret = sp_cursoropen @handle output,
  9647.             'select * from #spspeccol',
  9648.             @scrollopt output, @ccopt output, @rows output
  9649.         drop table #spspeccol
  9650.     end
  9651.     else if @procname = 'sp_sproc_columns'
  9652.     begin
  9653.         create table #spproccol (
  9654.             PROCEDURE_QUALIFIER varchar(32)  null,
  9655.             PROCEDURE_OWNER varchar(32)  null,
  9656.             PROCEDURE_NAME varchar(32)    not null,
  9657.             COLUMN_NAME varchar(32) not null,
  9658.             COLUMN_TYPE smallint not null,
  9659.             DATA_TYPE smallint not null,
  9660.             TYPE_NAME varchar(32) not null,
  9661.             "PRECISION" int null,
  9662.             LENGTH int null,
  9663.             SCALE smallint null,
  9664.             RADIX smallint null,
  9665.             NULLABLE smallint not null,
  9666.             REMARKS varchar(254) null,
  9667.             COLUMN_DEF varchar(255) null,
  9668.             SQL_DATA_TYPE smallint not null,
  9669.             SQL_DATETIME_SUB smallint null,
  9670.             CHAR_OCTET_LENGTH int null,
  9671.             ORDINAL_POSITION int not null,
  9672.             IS_NULLABLE varchar(254) null,
  9673.             SS_DATA_TYPE tinyint null
  9674.             )
  9675.         insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  9676.         exec @ret = sp_cursoropen @handle output,
  9677.             'select * from #spproccol',
  9678.             @scrollopt output, @ccopt output, @rows output
  9679.         drop table #spproccol
  9680.     end
  9681.     else if @procname = 'sp_statistics'
  9682.     begin
  9683.         create table #spstatistics (
  9684.             TABLE_QUALIFIER varchar(32)   null,
  9685.             TABLE_OWNER varchar(32)   null,
  9686.             TABLE_NAME varchar(32)    not null,
  9687.             NON_UNIQUE smallint null,
  9688.             INDEX_QUALIFIER varchar(32) null,
  9689.             INDEX_NAME varchar(32)    null,
  9690.             TYPE smallint not null,
  9691.             SEQ_IN_INDEX smallint null,
  9692.             COLUMN_NAME varchar(32) null,
  9693.             COLLATION char(1) null,
  9694.             CARDINALITY int null,
  9695.             PAGES int null,
  9696.             FILTER_CONDITION varchar(128) null
  9697.             )
  9698.         insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  9699.         exec @ret = sp_cursoropen @handle output,
  9700.             'select * from #spstatistics',
  9701.             @scrollopt output, @ccopt output, @rows output
  9702.         drop table #spstatistics
  9703.     end
  9704.     else if @procname = 'sp_stored_procedures'
  9705.     begin
  9706.         create table #spprocedures (
  9707.             PROCEDURE_QUALIFIER varchar(32)  null,
  9708.             PROCEDURE_OWNER varchar(32)  null,
  9709.             PROCEDURE_NAME varchar(32)    not null,
  9710.             NUM_INPUT_PARAMS int null,
  9711.             NUM_OUTPUT_PARAMS int null,
  9712.             NUM_RESULT_SETS int null,
  9713.             REMARKS varchar(254) null,
  9714.             PROCEDURE_TYPE smallint null
  9715.             )
  9716.         insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  9717.         exec @ret = sp_cursoropen @handle output,
  9718.             'select * from #spprocedures',
  9719.             @scrollopt output, @ccopt output, @rows output
  9720.         drop table #spprocedures
  9721.     end
  9722.     else if @procname = 'sp_table_privileges'
  9723.     begin
  9724.         create table #sptabpriv (
  9725.             TABLE_QUALIFIER varchar(32) null,
  9726.             TABLE_OWNER varchar(32) null,
  9727.             TABLE_NAME varchar(32)    not null,
  9728.             GRANTOR varchar(32) null,
  9729.             GRANTEE varchar(32) not null,
  9730.             PRIVILEGE varchar(32) not null,
  9731.             IS_GRANTABLE varchar(3) null
  9732.             )
  9733.         insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  9734.         exec @ret = sp_cursoropen @handle output,
  9735.             'select * from #sptabpriv',
  9736.             @scrollopt output, @ccopt output, @rows output
  9737.         drop table #sptabpriv
  9738.     end
  9739.     else if @procname = 'sp_tables'
  9740.     begin
  9741.         create table #sptables (
  9742.             TABLE_QUALIFIER varchar(32) null,
  9743.             TABLE_OWNER varchar(32) null,
  9744.             TABLE_NAME varchar(32)    null,
  9745.             TABLE_TYPE     varchar(32) null,
  9746.             REMARKS varchar(254) null)
  9747.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  9748.         exec @ret = sp_cursoropen @handle output,
  9749.             'select * from #sptables',
  9750.             @scrollopt output, @ccopt output, @rows output
  9751.         drop table #sptables
  9752.     end
  9753.     else
  9754.         print 'Unknown sp_ddopen procedure'
  9755.     select @ret = isnull(@ret,0)
  9756.     return isnull(@ret,0)
  9757. go
  9758.  
  9759. if (charindex('7.00', @@version) > 0 or
  9760.     charindex('8.00', @@version) > 0)
  9761.     drop procedure sp_ddopen
  9762. else
  9763. begin
  9764.     print ''
  9765.     print ''
  9766.     print 'Warning:'
  9767.     print 'you are installing the stored procedures '
  9768.     print 'on a pre 7.0 SQL Server.'
  9769.     print 'Ignore the following errors.'
  9770. end
  9771. go
  9772.  
  9773. /*    Procedure for 7.0 server */
  9774. create procedure sp_ddopen; 1(
  9775.                @handle            int output,
  9776.                @procname        sysname,
  9777.                @scrollopt        int output,
  9778.                @ccopt            int output,
  9779.                @rows            int output,
  9780.                @p1                nvarchar(774) = null,
  9781.                @p2                nvarchar(774) = null,
  9782.                @p3                nvarchar(774) = null,
  9783.                @p4                nvarchar(774) = null,
  9784.                @p5                nvarchar(774) = null,
  9785.                @p6                nvarchar(774) = null,
  9786.                @p7                int = null,
  9787.                @ODBCVer         int = 2)
  9788. as
  9789.     set nocount on
  9790.     declare @ret int
  9791.  
  9792.     if @procname = 'sp_column_privileges'
  9793.     begin
  9794.         exec @ret = sp_ddopen;2 @handle output, 
  9795.                             @scrollopt output, 
  9796.                             @ccopt output, 
  9797.                             @rows output, 
  9798.                             @p1,
  9799.                             @p2,
  9800.                             @p3,
  9801.                             @p4
  9802.     end
  9803.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  9804.     begin
  9805.         exec @ret = sp_ddopen;3 @handle output,
  9806.                             @procname,
  9807.                             @scrollopt output, 
  9808.                             @ccopt output, 
  9809.                             @rows output, 
  9810.                             @p1,
  9811.                             @p2,
  9812.                             @p3,
  9813.                             @p4,
  9814.                             @p5,
  9815.                             @ODBCVer
  9816.     end
  9817.     else if @procname = 'sp_datatype_info'
  9818.     begin
  9819.         exec @ret = sp_ddopen;4 @handle output,
  9820.                             @scrollopt output, 
  9821.                             @ccopt output, 
  9822.                             @rows output, 
  9823.                             @p7,
  9824.                             @ODBCVer
  9825.     end
  9826.     else if @procname = 'sp_fkeys'
  9827.     begin
  9828.         exec @ret = sp_ddopen;5 @handle output, 
  9829.                             @scrollopt output, 
  9830.                             @ccopt output, 
  9831.                             @rows output, 
  9832.                             @p1,
  9833.                             @p2,
  9834.                             @p3,
  9835.                             @p4,
  9836.                             @p5,
  9837.                             @p6
  9838.     end
  9839.     else if @procname = 'sp_pkeys'
  9840.     begin
  9841.         exec @ret = sp_ddopen;6 @handle output, 
  9842.                             @scrollopt output, 
  9843.                             @ccopt output, 
  9844.                             @rows output, 
  9845.                             @p1,
  9846.                             @p2,
  9847.                             @p3
  9848.     end
  9849.     else if @procname = 'sp_special_columns'
  9850.     begin
  9851.         exec @ret = sp_ddopen;7 @handle output, 
  9852.                             @scrollopt output, 
  9853.                             @ccopt output, 
  9854.                             @rows output, 
  9855.                             @p1,
  9856.                             @p2,
  9857.                             @p3,
  9858.                             @p4,
  9859.                             @p5,
  9860.                             @p6,
  9861.                             @ODBCVer
  9862.     end
  9863.     else if @procname = 'sp_sproc_columns'
  9864.     begin
  9865.         exec @ret = sp_ddopen;8 @handle output, 
  9866.                             @scrollopt output, 
  9867.                             @ccopt output, 
  9868.                             @rows output, 
  9869.                             @p1,
  9870.                             @p2,
  9871.                             @p3,
  9872.                             @p4,
  9873.                             @ODBCVer
  9874.     end
  9875.     else if @procname = 'sp_statistics'
  9876.     begin
  9877.         exec @ret = sp_ddopen;9 @handle output, 
  9878.                             @scrollopt output, 
  9879.                             @ccopt output, 
  9880.                             @rows output, 
  9881.                             @p1,
  9882.                             @p2,
  9883.                             @p3,
  9884.                             @p4,
  9885.                             @p5,
  9886.                             @p6
  9887.     end
  9888.     else if @procname = 'sp_stored_procedures'
  9889.     begin
  9890.         exec @ret = sp_ddopen;10 @handle output, 
  9891.                              @scrollopt output, 
  9892.                              @ccopt output, 
  9893.                              @rows output, 
  9894.                              @p1,
  9895.                              @p2,
  9896.                              @p3
  9897.     end
  9898.     else if @procname = 'sp_table_privileges'
  9899.     begin
  9900.         exec @ret = sp_ddopen;11 @handle output, 
  9901.                              @scrollopt output, 
  9902.                              @ccopt output, 
  9903.                              @rows output, 
  9904.                              @p1,
  9905.                              @p2,
  9906.                              @p3
  9907.     end
  9908.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  9909.     begin
  9910.         exec @ret = sp_ddopen;12 @handle output,
  9911.                             @procname,
  9912.                             @scrollopt output, 
  9913.                             @ccopt output, 
  9914.                             @rows output, 
  9915.                             @p1,
  9916.                             @p2,
  9917.                             @p3,
  9918.                             @p4,
  9919.                             @p5
  9920.     end
  9921.     else if @procname = 'sp_tableswc'
  9922.     begin
  9923.         exec @ret = sp_ddopen;13 @handle output,
  9924.                             @procname,
  9925.                             @scrollopt output,
  9926.                             @ccopt output,
  9927.                             @rows output,
  9928.                             @p1,
  9929.                             @p2,
  9930.                             @p3,
  9931.                             @p4
  9932.     end
  9933.     else
  9934.         print 'Unknown sp_ddopen procedure'
  9935.     select @ret = isnull(@ret,0)
  9936.     return isnull(@ret,0)
  9937. go
  9938.  
  9939.  
  9940. if (charindex('7.00', @@version) = 0 and
  9941.     charindex('8.00', @@version) = 0)
  9942. begin
  9943.     print ''
  9944.     print ''
  9945.     print 'Warning:'
  9946.     print 'you are installing the stored procedures '
  9947.     print 'on a pre 7.0 SQL Server.'
  9948.     print 'Ignore the following errors.'
  9949. end
  9950. go
  9951.  
  9952. create procedure sp_ddopen; 2(
  9953.                @handle            int output,
  9954.                @scrollopt        int output,
  9955.                @ccopt            int output,
  9956.                @rows            int output,
  9957.                @p1                nvarchar(774),
  9958.                @p2                nvarchar(774),
  9959.                @p3                nvarchar(774),
  9960.                @p4                nvarchar(774))
  9961. as
  9962.     set nocount on
  9963.     declare @ret int
  9964.  
  9965.     create table #spcolpriv (
  9966.         TABLE_QUALIFIER sysname null,
  9967.         TABLE_OWNER sysname null,
  9968.         TABLE_NAME sysname not null,
  9969.         COLUMN_NAME sysname not null,
  9970.         GRANTOR sysname null,
  9971.         GRANTEE sysname not null,
  9972.         PRIVILEGE varchar(32) not null,
  9973.         IS_GRANTABLE varchar(3) null
  9974.         )
  9975.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  9976.     exec @ret = sp_cursoropen @handle output,
  9977.         'select * from #spcolpriv',
  9978.         @scrollopt output, @ccopt output, @rows output
  9979.     drop table #spcolpriv
  9980.     return @ret
  9981. go
  9982.  
  9983.  
  9984. if (charindex('7.00', @@version) = 0 and
  9985.     charindex('8.00', @@version) = 0)
  9986. begin
  9987.     print ''
  9988.     print ''
  9989.     print 'Warning:'
  9990.     print 'you are installing the stored procedures '
  9991.     print 'on a pre 7.0 SQL Server.'
  9992.     print 'Ignore the following errors.'
  9993. end
  9994. go
  9995.  
  9996. create procedure sp_ddopen; 3(
  9997.                @handle            int output,
  9998.                @procname        sysname,
  9999.                @scrollopt        int output,
  10000.                @ccopt            int output,
  10001.                @rows            int output,
  10002.                @p1                nvarchar(774),
  10003.                @p2                nvarchar(774),
  10004.                @p3                nvarchar(774),
  10005.                @p4                nvarchar(774),
  10006.                @p5                nvarchar(774),
  10007.                @ODBCVer         int)
  10008. as
  10009.     set nocount on
  10010.     declare @ret int
  10011.  
  10012.     create table #spcolumns (
  10013.         TABLE_QUALIFIER sysname null,
  10014.         TABLE_OWNER sysname null,
  10015.         TABLE_NAME sysname not null,
  10016.         COLUMN_NAME sysname not null,
  10017.         DATA_TYPE smallint not null,
  10018.         TYPE_NAME sysname not null,
  10019.         "PRECISION" int null,
  10020.         LENGTH int null,
  10021.         SCALE smallint null,
  10022.         RADIX smallint null,
  10023.         NULLABLE smallint not null,
  10024.         REMARKS varchar(254) null,
  10025.         COLUMN_DEF nvarchar(3000) null,
  10026.         SQL_DATA_TYPE smallint not null,
  10027.         SQL_DATETIME_SUB smallint null,
  10028.         CHAR_OCTET_LENGTH int null,
  10029.         ORDINAL_POSITION int not null,
  10030.         IS_NULLABLE varchar(254) null,
  10031.         SS_DATA_TYPE tinyint null
  10032.         )
  10033.  
  10034.     if @procname = 'sp_columns'
  10035.     begin
  10036.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10037.     end
  10038.     else
  10039.     begin
  10040.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10041.     end
  10042.     exec @ret = sp_cursoropen @handle output,
  10043.         'select * from #spcolumns',
  10044.         @scrollopt output, @ccopt output, @rows output
  10045.     drop table #spcolumns
  10046.     return @ret
  10047. go
  10048.  
  10049.  
  10050.  
  10051. if (charindex('7.00', @@version) = 0 and
  10052.     charindex('8.00', @@version) = 0)
  10053. begin
  10054.     print ''
  10055.     print ''
  10056.     print 'Warning:'
  10057.     print 'you are installing the stored procedures '
  10058.     print 'on a pre 7.0 SQL Server.'
  10059.     print 'Ignore the following errors.'
  10060. end
  10061. go
  10062.  
  10063. create procedure sp_ddopen; 4(
  10064.                @handle            int output,
  10065.                @scrollopt        int output,
  10066.                @ccopt            int output,
  10067.                @rows            int output,
  10068.                @p7                int,
  10069.                @ODBCVer         int)
  10070. as
  10071.     set nocount on
  10072.     declare @ret int
  10073.  
  10074.     create table #spdatatypeinfo (
  10075.         TYPE_NAME            sysname  not null,
  10076.         DATA_TYPE            smallint not null,
  10077.         "PRECISION"            int null,
  10078.         LITERAL_PREFIX        varchar(32)    null,
  10079.         LITERAL_SUFFIX        varchar(32)    null,
  10080.         CREATE_PARAMS        varchar(32)    null,
  10081.         NULLABLE            smallint   not null,
  10082.         CASE_SENSITIVE        smallint   not null,
  10083.         SEARCHABLE            smallint   not null,
  10084.         UNSIGNED_ATTRIBUTE    smallint   null,
  10085.         MONEY    smallint    not null,
  10086.         AUTO_INCREMENT        smallint    null,
  10087.         LOCAL_TYPE_NAME     sysname null,
  10088.         MINIMUM_SCALE        smallint     null,
  10089.         MAXIMUM_SCALE        smallint   null,
  10090.         SQL_DATA_TYPE        smallint      not null,
  10091.         SQL_DATETIME_SUB    smallint   null,
  10092.         NUM_PREC_RADIX        int     null,
  10093.         INTERVAL_PRECISION    smallint    NULL,
  10094.         USERTYPE            smallint not null)
  10095.  
  10096.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10097.     exec @ret = sp_cursoropen @handle output,
  10098.         'select * from #spdatatypeinfo',
  10099.         @scrollopt output, @ccopt output, @rows output
  10100.     drop table #spdatatypeinfo
  10101.     return @ret
  10102. go
  10103.  
  10104.  
  10105.  
  10106. if (charindex('7.00', @@version) = 0 and
  10107.     charindex('8.00', @@version) = 0)
  10108. begin
  10109.     print ''
  10110.     print ''
  10111.     print 'Warning:'
  10112.     print 'you are installing the stored procedures '
  10113.     print 'on a pre 7.0 SQL Server.'
  10114.     print 'Ignore the following errors.'
  10115. end
  10116. go
  10117.  
  10118. create procedure sp_ddopen; 5(
  10119.                @handle            int output,
  10120.                @scrollopt        int output,
  10121.                @ccopt            int output,
  10122.                @rows            int output,
  10123.                @p1                nvarchar(774),
  10124.                @p2                nvarchar(774),
  10125.                @p3                nvarchar(774),
  10126.                @p4                nvarchar(774),
  10127.                @p5                nvarchar(774),
  10128.                @p6                nvarchar(774))
  10129. as
  10130.     set nocount on
  10131.     declare @ret int
  10132.  
  10133.     create table #spfkeys (
  10134.         PKTABLE_QUALIFIER sysname     null,
  10135.         PKTABLE_OWNER sysname    null,
  10136.         PKTABLE_NAME sysname  not null,
  10137.         PKCOLUMN_NAME sysname  not null,
  10138.         FKTABLE_QUALIFIER sysname    null,
  10139.         FKTABLE_OWNER sysname    null,
  10140.         FKTABLE_NAME sysname  not null,
  10141.         FKCOLUMN_NAME sysname  not null,
  10142.         KEY_SEQ smallint not null,
  10143.         UPDATE_RULE smallint null,
  10144.         DELETE_RULE smallint null,
  10145.         FK_NAME sysname null,
  10146.         PK_NAME sysname null,
  10147.         DEFERRABILITY smallint null
  10148.         )
  10149.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10150.     exec @ret = sp_cursoropen @handle output,
  10151.         'select * from #spfkeys',
  10152.         @scrollopt output, @ccopt output, @rows output
  10153.     drop table #spfkeys
  10154.     return @ret
  10155. go
  10156.  
  10157.  
  10158.  
  10159. if (charindex('7.00', @@version) = 0 and
  10160.     charindex('8.00', @@version) = 0)
  10161. begin
  10162.     print ''
  10163.     print ''
  10164.     print 'Warning:'
  10165.     print 'you are installing the stored procedures '
  10166.     print 'on a pre 7.0 SQL Server.'
  10167.     print 'Ignore the following errors.'
  10168. end
  10169. go
  10170.  
  10171. create procedure sp_ddopen; 6(
  10172.                @handle            int output,
  10173.                @scrollopt        int output,
  10174.                @ccopt            int output,
  10175.                @rows            int output,
  10176.                @p1                nvarchar(774),
  10177.                @p2                nvarchar(774),
  10178.                @p3                nvarchar(774))
  10179. as
  10180.     set nocount on
  10181.     declare @ret int
  10182.  
  10183.     create table #sppkeys (
  10184.         TABLE_QUALIFIER sysname   null,
  10185.         TABLE_OWNER sysname   null,
  10186.         TABLE_NAME sysname    not null,
  10187.         COLUMN_NAME sysname  not null,
  10188.         KEY_SEQ smallint not null,
  10189.         PK_NAME sysname null
  10190.         )
  10191.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  10192.     exec @ret = sp_cursoropen @handle output,
  10193.         'select * from #sppkeys',
  10194.         @scrollopt output, @ccopt output, @rows output
  10195.     drop table #sppkeys
  10196.     return @ret
  10197. go
  10198.  
  10199.  
  10200.  
  10201. if (charindex('7.00', @@version) = 0 and
  10202.     charindex('8.00', @@version) = 0)
  10203. begin
  10204.     print ''
  10205.     print ''
  10206.     print 'Warning:'
  10207.     print 'you are installing the stored procedures '
  10208.     print 'on a pre 7.0 SQL Server.'
  10209.     print 'Ignore the following errors.'
  10210. end
  10211. go
  10212.  
  10213. create procedure sp_ddopen; 7(
  10214.                @handle            int output,
  10215.                @scrollopt        int output,
  10216.                @ccopt            int output,
  10217.                @rows            int output,
  10218.                @p1                nvarchar(774),
  10219.                @p2                nvarchar(774),
  10220.                @p3                nvarchar(774),
  10221.                @p4                nvarchar(774),
  10222.                @p5                nvarchar(774),
  10223.                @p6                nvarchar(774),
  10224.                @ODBCVer         int)
  10225. as
  10226.     set nocount on
  10227.     declare @ret int
  10228.  
  10229.     create table #spspeccol (
  10230.         SCOPE smallint null,
  10231.         COLUMN_NAME sysname not null,
  10232.         DATA_TYPE smallint not null,
  10233.         TYPE_NAME sysname not null,
  10234.         "PRECISION" int null,
  10235.         LENGTH int null,
  10236.         SCALE smallint null,
  10237.         PSEUDO_COLUMN smallint null
  10238.         )
  10239.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  10240.     exec @ret = sp_cursoropen @handle output,
  10241.         'select * from #spspeccol',
  10242.         @scrollopt output, @ccopt output, @rows output
  10243.     drop table #spspeccol
  10244.     return @ret
  10245. go
  10246.  
  10247.  
  10248.  
  10249. if (charindex('7.00', @@version) = 0 and
  10250.     charindex('8.00', @@version) = 0)
  10251. begin
  10252.     print ''
  10253.     print ''
  10254.     print 'Warning:'
  10255.     print 'you are installing the stored procedures '
  10256.     print 'on a pre 7.0 SQL Server.'
  10257.     print 'Ignore the following errors.'
  10258. end
  10259. go
  10260.  
  10261. create procedure sp_ddopen; 8(
  10262.                @handle            int output,
  10263.                @scrollopt        int output,
  10264.                @ccopt            int output,
  10265.                @rows            int output,
  10266.                @p1                nvarchar(774),
  10267.                @p2                nvarchar(774),
  10268.                @p3                nvarchar(774),
  10269.                @p4                nvarchar(774),
  10270.                @ODBCVer         int)
  10271. as
  10272.     set nocount on
  10273.     declare @ret int
  10274.  
  10275.     create table #spproccol (
  10276.         PROCEDURE_QUALIFIER sysname  null,
  10277.         PROCEDURE_OWNER sysname  null,
  10278.         PROCEDURE_NAME sysname not null,
  10279.         COLUMN_NAME sysname not null,
  10280.         COLUMN_TYPE smallint not null,
  10281.         DATA_TYPE smallint not null,
  10282.         TYPE_NAME sysname not null,
  10283.         "PRECISION" int null,
  10284.         LENGTH int null,
  10285.         SCALE smallint null,
  10286.         RADIX smallint null,
  10287.         NULLABLE smallint not null,
  10288.         REMARKS varchar(254) null,
  10289.         COLUMN_DEF nvarchar(3000) null,
  10290.         SQL_DATA_TYPE smallint not null,
  10291.         SQL_DATETIME_SUB smallint null,
  10292.         CHAR_OCTET_LENGTH int null,
  10293.         ORDINAL_POSITION int not null,
  10294.         IS_NULLABLE varchar(254) null,
  10295.         SS_DATA_TYPE tinyint null
  10296.         )
  10297.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  10298.     exec @ret = sp_cursoropen @handle output,
  10299.         'select * from #spproccol',
  10300.         @scrollopt output, @ccopt output, @rows output
  10301.     drop table #spproccol
  10302.     return @ret
  10303. go
  10304.  
  10305.  
  10306.  
  10307. if (charindex('7.00', @@version) = 0 and
  10308.     charindex('8.00', @@version) = 0)
  10309. begin
  10310.     print ''
  10311.     print ''
  10312.     print 'Warning:'
  10313.     print 'you are installing the stored procedures '
  10314.     print 'on a pre 7.0 SQL Server.'
  10315.     print 'Ignore the following errors.'
  10316. end
  10317. go
  10318.  
  10319. create procedure sp_ddopen; 9(
  10320.                @handle            int output,
  10321.                @scrollopt        int output,
  10322.                @ccopt            int output,
  10323.                @rows            int output,
  10324.                @p1                nvarchar(774),
  10325.                @p2                nvarchar(774),
  10326.                @p3                nvarchar(774),
  10327.                @p4                nvarchar(774),
  10328.                @p5                nvarchar(774),
  10329.                @p6                nvarchar(774))
  10330. as
  10331.     set nocount on
  10332.     declare @ret int
  10333.  
  10334.     create table #spstatistics (
  10335.         TABLE_QUALIFIER sysname   null,
  10336.         TABLE_OWNER sysname   null,
  10337.         TABLE_NAME sysname    not null,
  10338.         NON_UNIQUE smallint null,
  10339.         INDEX_QUALIFIER sysname null,
  10340.         INDEX_NAME sysname null,
  10341.         TYPE smallint not null,
  10342.         SEQ_IN_INDEX smallint null,
  10343.         COLUMN_NAME sysname null,
  10344.         COLLATION char(1) null,
  10345.         CARDINALITY int null,
  10346.         PAGES int null,
  10347.         FILTER_CONDITION varchar(128) null
  10348.         )
  10349.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  10350.     exec @ret = sp_cursoropen @handle output,
  10351.         'select * from #spstatistics',
  10352.         @scrollopt output, @ccopt output, @rows output
  10353.     drop table #spstatistics
  10354.     return @ret
  10355. go
  10356.                
  10357.  
  10358.  
  10359. if (charindex('7.00', @@version) = 0 and
  10360.     charindex('8.00', @@version) = 0)
  10361. begin
  10362.     print ''
  10363.     print ''
  10364.     print 'Warning:'
  10365.     print 'you are installing the stored procedures '
  10366.     print 'on a pre 7.0 SQL Server.'
  10367.     print 'Ignore the following errors.'
  10368. end
  10369. go
  10370.  
  10371. create procedure sp_ddopen; 10(
  10372.                @handle            int output,
  10373.                @scrollopt            int output,
  10374.                @ccopt            int output,
  10375.                @rows            int output,
  10376.                @p1                nvarchar(774),
  10377.                @p2                nvarchar(774),
  10378.                @p3                nvarchar(774))
  10379. as
  10380.     set nocount on
  10381.     declare @ret int
  10382.  
  10383.     create table #spprocedures (
  10384.         PROCEDURE_QUALIFIER sysname  null,
  10385.         PROCEDURE_OWNER sysname  null,
  10386.         PROCEDURE_NAME nvarchar(134) not null, /*134=sysname+';'+ltrim(str(c.number,5))*/
  10387.         NUM_INPUT_PARAMS int null,
  10388.         NUM_OUTPUT_PARAMS int null,
  10389.         NUM_RESULT_SETS int null,
  10390.         REMARKS varchar(254) null,
  10391.         PROCEDURE_TYPE smallint null
  10392.         )
  10393.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  10394.     exec @ret = sp_cursoropen @handle output,
  10395.         'select * from #spprocedures',
  10396.         @scrollopt output, @ccopt output, @rows output
  10397.     drop table #spprocedures
  10398.     return @ret
  10399. go
  10400.  
  10401.  
  10402.  
  10403. if (charindex('7.00', @@version) = 0 and
  10404.     charindex('8.00', @@version) = 0)
  10405. begin
  10406.     print ''
  10407.     print ''
  10408.     print 'Warning:'
  10409.     print 'you are installing the stored procedures '
  10410.     print 'on a pre 7.0 SQL Server.'
  10411.     print 'Ignore the following errors.'
  10412. end
  10413. go
  10414.  
  10415. create procedure sp_ddopen; 11(
  10416.                @handle            int output,
  10417.                @scrollopt        int output,
  10418.                @ccopt            int output,
  10419.                @rows            int output,
  10420.                @p1                nvarchar(774),
  10421.                @p2                nvarchar(774),
  10422.                @p3                nvarchar(774))
  10423. as
  10424.     set nocount on
  10425.     declare @ret int
  10426.  
  10427.     create table #sptabpriv (
  10428.         TABLE_QUALIFIER sysname null,
  10429.         TABLE_OWNER sysname null,
  10430.         TABLE_NAME sysname not null,
  10431.         GRANTOR sysname null,
  10432.         GRANTEE sysname not null,
  10433.         PRIVILEGE varchar(32) not null,
  10434.         IS_GRANTABLE varchar(3) null
  10435.         )
  10436.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  10437.     exec @ret = sp_cursoropen @handle output,
  10438.         'select * from #sptabpriv',
  10439.         @scrollopt output, @ccopt output, @rows output
  10440.     drop table #sptabpriv
  10441.     return @ret
  10442. go
  10443.  
  10444.  
  10445.  
  10446. if (charindex('7.00', @@version) = 0 and
  10447.     charindex('8.00', @@version) = 0)
  10448. begin
  10449.     print ''
  10450.     print ''
  10451.     print 'Warning:'
  10452.     print 'you are installing the stored procedures '
  10453.     print 'on a pre 7.0 SQL Server.'
  10454.     print 'Ignore the following errors.'
  10455. end
  10456. go
  10457.  
  10458. create procedure sp_ddopen; 12(
  10459.                @handle            int output,
  10460.                @procname        sysname,
  10461.                @scrollopt        int output,
  10462.                @ccopt            int output,
  10463.                @rows            int output,
  10464.                @p1                nvarchar(774),
  10465.                @p2                nvarchar(774),
  10466.                @p3                nvarchar(774),
  10467.                @p4                nvarchar(774),
  10468.                @p5                nvarchar(774))
  10469. as
  10470.     set nocount on
  10471.     declare @ret int
  10472.  
  10473.     create table #sptables (
  10474.         TABLE_QUALIFIER sysname null,
  10475.         TABLE_OWNER sysname null,
  10476.         TABLE_NAME sysname null,
  10477.         TABLE_TYPE    varchar(32) null,
  10478.         REMARKS varchar(254) null)
  10479.     if @procname = 'sp_tables'
  10480.     begin
  10481.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  10482.     end
  10483.     else
  10484.     begin
  10485.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  10486.     end
  10487.     exec @ret = sp_cursoropen @handle output,
  10488.         'select * from #sptables',
  10489.         @scrollopt output, @ccopt output, @rows output
  10490.     drop table #sptables
  10491.     return @ret
  10492. go
  10493.  
  10494. if (charindex('7.00', @@version) = 0 and
  10495.     charindex('8.00', @@version) = 0)
  10496. begin
  10497.     print ''
  10498.     print ''
  10499.     print 'Warning:'
  10500.     print 'you are installing the stored procedures '
  10501.     print 'on a pre 7.0 SQL Server.'
  10502.     print 'Ignore the following errors.'
  10503. end
  10504. go
  10505.  
  10506. create procedure sp_ddopen; 13(
  10507.                @handle          int output,
  10508.                @procname        sysname,
  10509.                @scrollopt       int output,
  10510.                @ccopt           int output,
  10511.                @rows            int output,
  10512.                @p1              nvarchar(774),  -- @table_name
  10513.                @p2              nvarchar(774),  -- @table_owner
  10514.                @p3              nvarchar(774),  -- @table_qualifier
  10515.                @p4              nvarchar(774))  -- @table_type
  10516. as
  10517.     -- this code was copied from sp_tableswc (for 7,0).  Any changes
  10518.     -- made here need to be evaluated for porting to sp_tableswc.
  10519.     set nocount on
  10520.     declare @ret int
  10521.     declare @databasename   sysname
  10522.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  10523.     
  10524.     create table #sptables (
  10525.         TABLE_QUALIFIER sysname null,
  10526.         TABLE_OWNER sysname null,
  10527.         TABLE_NAME sysname null,
  10528.         TABLE_TYPE varchar(32) null,
  10529.         REMARKS varchar(254) null)
  10530.         
  10531.     declare databases CURSOR FOR
  10532.         select name from master..sysdatabases
  10533.         where name like @p3 and name <> 'model' and has_dbaccess(name)=1
  10534.         for read only
  10535.  
  10536.     open databases
  10537.     fetch next from databases into @databasename
  10538.     while (@@FETCH_STATUS <> -1)
  10539.     begin
  10540.         if (charindex('%', @databasename) = 0)
  10541.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  10542.             select @qualprocname = @databasename + '..sp_tables'
  10543.             insert into #sptables exec @qualprocname
  10544.                 @p1, @p2, @databasename, @p3
  10545.         end
  10546.         fetch next from databases into @databasename
  10547.     end
  10548.     deallocate databases
  10549.     
  10550.     exec @ret = sp_cursoropen @handle output,
  10551.         'select * from #sptables',
  10552.         @scrollopt output, @ccopt output, @rows output
  10553.     drop table #sptables
  10554.     return @ret
  10555. go
  10556.  
  10557. if (charindex('8.00', @@version) > 0)
  10558.     drop procedure sp_ddopen
  10559. else
  10560. begin
  10561.     print ''
  10562.     print ''
  10563.     print 'Warning:'
  10564.     print 'you are installing the stored procedures '
  10565.     print 'on a pre 8.0 SQL Server.'
  10566.     print 'Ignore the following errors.'
  10567. end
  10568. go
  10569.  
  10570. /*    Procedure for 8.0 server */
  10571. create procedure sp_ddopen; 1(
  10572.                @handle            int output,
  10573.                @procname        sysname,
  10574.                @scrollopt        int output,
  10575.                @ccopt            int output,
  10576.                @rows            int output,
  10577.                @p1                nvarchar(774) = null,
  10578.                @p2                nvarchar(774) = null,
  10579.                @p3                nvarchar(774) = null,
  10580.                @p4                nvarchar(774) = null,
  10581.                @p5                nvarchar(774) = null,
  10582.                @p6                nvarchar(774) = null,
  10583.                @p7                int = null,
  10584.                @ODBCVer         int = 2)
  10585. as
  10586.     set nocount on
  10587.     declare @ret int
  10588.  
  10589.     if @procname = 'sp_column_privileges'
  10590.     begin
  10591.         exec @ret = sp_ddopen;2 @handle output, 
  10592.                             @scrollopt output, 
  10593.                             @ccopt output, 
  10594.                             @rows output, 
  10595.                             @p1,
  10596.                             @p2,
  10597.                             @p3,
  10598.                             @p4
  10599.     end
  10600.     else if @procname = 'sp_columns' or @procname = 'sp_columns_ex'
  10601.     begin
  10602.         exec @ret = sp_ddopen;3 @handle output,
  10603.                             @procname,
  10604.                             @scrollopt output, 
  10605.                             @ccopt output, 
  10606.                             @rows output, 
  10607.                             @p1,
  10608.                             @p2,
  10609.                             @p3,
  10610.                             @p4,
  10611.                             @p5,
  10612.                             @ODBCVer
  10613.     end
  10614.     else if @procname = 'sp_datatype_info'
  10615.     begin
  10616.         exec @ret = sp_ddopen;4 @handle output,
  10617.                             @scrollopt output, 
  10618.                             @ccopt output, 
  10619.                             @rows output, 
  10620.                             @p7,
  10621.                             @ODBCVer
  10622.     end
  10623.     else if @procname = 'sp_fkeys'
  10624.     begin
  10625.         exec @ret = sp_ddopen;5 @handle output, 
  10626.                             @scrollopt output, 
  10627.                             @ccopt output, 
  10628.                             @rows output, 
  10629.                             @p1,
  10630.                             @p2,
  10631.                             @p3,
  10632.                             @p4,
  10633.                             @p5,
  10634.                             @p6
  10635.     end
  10636.     else if @procname = 'sp_pkeys'
  10637.     begin
  10638.         exec @ret = sp_ddopen;6 @handle output, 
  10639.                             @scrollopt output, 
  10640.                             @ccopt output, 
  10641.                             @rows output, 
  10642.                             @p1,
  10643.                             @p2,
  10644.                             @p3
  10645.     end
  10646.     else if @procname = 'sp_special_columns'
  10647.     begin
  10648.         exec @ret = sp_ddopen;7 @handle output, 
  10649.                             @scrollopt output, 
  10650.                             @ccopt output, 
  10651.                             @rows output, 
  10652.                             @p1,
  10653.                             @p2,
  10654.                             @p3,
  10655.                             @p4,
  10656.                             @p5,
  10657.                             @p6,
  10658.                             @ODBCVer
  10659.     end
  10660.     else if @procname = 'sp_sproc_columns'
  10661.     begin
  10662.         exec @ret = sp_ddopen;8 @handle output, 
  10663.                             @scrollopt output, 
  10664.                             @ccopt output, 
  10665.                             @rows output, 
  10666.                             @p1,
  10667.                             @p2,
  10668.                             @p3,
  10669.                             @p4,
  10670.                             @ODBCVer
  10671.     end
  10672.     else if @procname = 'sp_statistics'
  10673.     begin
  10674.         exec @ret = sp_ddopen;9 @handle output, 
  10675.                             @scrollopt output, 
  10676.                             @ccopt output, 
  10677.                             @rows output, 
  10678.                             @p1,
  10679.                             @p2,
  10680.                             @p3,
  10681.                             @p4,
  10682.                             @p5,
  10683.                             @p6
  10684.     end
  10685.     else if @procname = 'sp_stored_procedures'
  10686.     begin
  10687.         exec @ret = sp_ddopen;10 @handle output, 
  10688.                              @scrollopt output, 
  10689.                              @ccopt output, 
  10690.                              @rows output, 
  10691.                              @p1,
  10692.                              @p2,
  10693.                              @p3
  10694.     end
  10695.     else if @procname = 'sp_table_privileges'
  10696.     begin
  10697.         exec @ret = sp_ddopen;11 @handle output, 
  10698.                              @scrollopt output, 
  10699.                              @ccopt output, 
  10700.                              @rows output, 
  10701.                              @p1,
  10702.                              @p2,
  10703.                              @p3
  10704.     end
  10705.     else if @procname = 'sp_tables' or @procname = 'sp_tables_ex'
  10706.     begin
  10707.         exec @ret = sp_ddopen;12 @handle output,
  10708.                             @procname,
  10709.                             @scrollopt output, 
  10710.                             @ccopt output, 
  10711.                             @rows output, 
  10712.                             @p1,
  10713.                             @p2,
  10714.                             @p3,
  10715.                             @p4,
  10716.                             @p5
  10717.     end
  10718.     else if @procname = 'sp_tableswc'
  10719.     begin
  10720.         exec @ret = sp_ddopen;13 @handle output,
  10721.                             @procname,
  10722.                             @scrollopt output,
  10723.                             @ccopt output,
  10724.                             @rows output,
  10725.                             @p1,
  10726.                             @p2,
  10727.                             @p3,
  10728.                             @p4
  10729.     end
  10730.     else
  10731.         print 'Unknown sp_ddopen procedure'
  10732.     select @ret = isnull(@ret,0)
  10733.     return isnull(@ret,0)
  10734. go
  10735.  
  10736.  
  10737. if (charindex('8.00', @@version) = 0)
  10738. begin
  10739.     print ''
  10740.     print ''
  10741.     print 'Warning:'
  10742.     print 'you are installing the stored procedures '
  10743.     print 'on a pre 8.0 SQL Server.'
  10744.     print 'Ignore the following errors.'
  10745. end
  10746. go
  10747.  
  10748. create procedure sp_ddopen; 2(
  10749.                @handle            int output,
  10750.                @scrollopt        int output,
  10751.                @ccopt            int output,
  10752.                @rows            int output,
  10753.                @p1                nvarchar(774),
  10754.                @p2                nvarchar(774),
  10755.                @p3                nvarchar(774),
  10756.                @p4                nvarchar(774))
  10757. as
  10758.     set nocount on
  10759.     declare @ret int
  10760.  
  10761.     create table #spcolpriv (
  10762.         TABLE_QUALIFIER sysname collate database_default null,
  10763.         TABLE_OWNER sysname collate database_default null,
  10764.         TABLE_NAME sysname collate database_default not null,
  10765.         COLUMN_NAME sysname collate database_default not null,
  10766.         GRANTOR sysname collate database_default null,
  10767.         GRANTEE sysname collate database_default not null,
  10768.         PRIVILEGE varchar(32) collate database_default not null,
  10769.         IS_GRANTABLE varchar(3) collate database_default null
  10770.         )
  10771.     insert into #spcolpriv exec sp_column_privileges @p1,@p2,@p3,@p4
  10772.     exec @ret = sp_cursoropen @handle output,
  10773.         'select * from #spcolpriv',
  10774.         @scrollopt output, @ccopt output, @rows output
  10775.     drop table #spcolpriv
  10776.     return @ret
  10777. go
  10778.  
  10779.  
  10780. if (charindex('8.00', @@version) = 0)
  10781. begin
  10782.     print ''
  10783.     print ''
  10784.     print 'Warning:'
  10785.     print 'you are installing the stored procedures '
  10786.     print 'on a pre 8.0 SQL Server.'
  10787.     print 'Ignore the following errors.'
  10788. end
  10789. go
  10790.  
  10791. create procedure sp_ddopen; 3(
  10792.                @handle            int output,
  10793.                @procname        sysname,
  10794.                @scrollopt        int output,
  10795.                @ccopt            int output,
  10796.                @rows            int output,
  10797.                @p1                nvarchar(774),
  10798.                @p2                nvarchar(774),
  10799.                @p3                nvarchar(774),
  10800.                @p4                nvarchar(774),
  10801.                @p5                nvarchar(774),
  10802.                @ODBCVer         int)
  10803. as
  10804.     set nocount on
  10805.     declare @ret int
  10806.  
  10807.     create table #spcolumns (
  10808.         TABLE_QUALIFIER sysname collate database_default null,
  10809.         TABLE_OWNER sysname collate database_default null,
  10810.         TABLE_NAME sysname collate database_default not null,
  10811.         COLUMN_NAME sysname collate database_default not null,
  10812.         DATA_TYPE smallint not null,
  10813.         TYPE_NAME sysname collate database_default not null,
  10814.         "PRECISION" int null,
  10815.         LENGTH int null,
  10816.         SCALE smallint null,
  10817.         RADIX smallint null,
  10818.         NULLABLE smallint not null,
  10819.         REMARKS varchar(254) collate database_default null,
  10820.         COLUMN_DEF nvarchar(3000) collate database_default null,
  10821.         SQL_DATA_TYPE smallint null,
  10822.         SQL_DATETIME_SUB smallint null,
  10823.         CHAR_OCTET_LENGTH int null,
  10824.         ORDINAL_POSITION int not null,
  10825.         IS_NULLABLE varchar(254) collate database_default null,
  10826.         SS_DATA_TYPE tinyint null
  10827.         )
  10828.  
  10829.     if @procname = 'sp_columns'
  10830.     begin
  10831.         insert into #spcolumns exec sp_columns @p1,@p2,@p3,@p4,@ODBCVer
  10832.     end
  10833.     else
  10834.     begin
  10835.         insert into #spcolumns exec sp_columns_ex @p1,@p2,@p3,@p4,@p5,@ODBCVer
  10836.     end
  10837.     exec @ret = sp_cursoropen @handle output,
  10838.         'select * from #spcolumns',
  10839.         @scrollopt output, @ccopt output, @rows output
  10840.     drop table #spcolumns
  10841.     return @ret
  10842. go
  10843.  
  10844.  
  10845.  
  10846. if (charindex('8.00', @@version) = 0)
  10847. begin
  10848.     print ''
  10849.     print ''
  10850.     print 'Warning:'
  10851.     print 'you are installing the stored procedures '
  10852.     print 'on a pre 8.0 SQL Server.'
  10853.     print 'Ignore the following errors.'
  10854. end
  10855. go
  10856.  
  10857. create procedure sp_ddopen; 4(
  10858.                @handle            int output,
  10859.                @scrollopt        int output,
  10860.                @ccopt            int output,
  10861.                @rows            int output,
  10862.                @p7                int,
  10863.                @ODBCVer         int)
  10864. as
  10865.     set nocount on
  10866.     declare @ret int
  10867.  
  10868.     create table #spdatatypeinfo (
  10869.         TYPE_NAME            sysname  collate database_default not null,
  10870.         DATA_TYPE            smallint not null,
  10871.         "PRECISION"            int null,
  10872.         LITERAL_PREFIX        varchar(32)    collate database_default null,
  10873.         LITERAL_SUFFIX        varchar(32)    collate database_default null,
  10874.         CREATE_PARAMS        varchar(32)    collate database_default null,
  10875.         NULLABLE            smallint   not null,
  10876.         CASE_SENSITIVE        smallint   not null,
  10877.         SEARCHABLE            smallint   not null,
  10878.         UNSIGNED_ATTRIBUTE    smallint   null,
  10879.         MONEY    smallint    not null,
  10880.         AUTO_INCREMENT        smallint    null,
  10881.         LOCAL_TYPE_NAME     sysname collate database_default null,
  10882.         MINIMUM_SCALE        smallint     null,
  10883.         MAXIMUM_SCALE        smallint   null,
  10884.         SQL_DATA_TYPE        smallint      not null,
  10885.         SQL_DATETIME_SUB    smallint   null,
  10886.         NUM_PREC_RADIX        int     null,
  10887.         INTERVAL_PRECISION    smallint    NULL,
  10888.         USERTYPE            smallint not null)
  10889.  
  10890.     insert into #spdatatypeinfo exec sp_datatype_info @p7,@ODBCVer
  10891.     exec @ret = sp_cursoropen @handle output,
  10892.         'select * from #spdatatypeinfo',
  10893.         @scrollopt output, @ccopt output, @rows output
  10894.     drop table #spdatatypeinfo
  10895.     return @ret
  10896. go
  10897.  
  10898.  
  10899.  
  10900. if (charindex('8.00', @@version) = 0)
  10901. begin
  10902.     print ''
  10903.     print ''
  10904.     print 'Warning:'
  10905.     print 'you are installing the stored procedures '
  10906.     print 'on a pre 8.0 SQL Server.'
  10907.     print 'Ignore the following errors.'
  10908. end
  10909. go
  10910.  
  10911. create procedure sp_ddopen; 5(
  10912.                @handle            int output,
  10913.                @scrollopt        int output,
  10914.                @ccopt            int output,
  10915.                @rows            int output,
  10916.                @p1                nvarchar(774),
  10917.                @p2                nvarchar(774),
  10918.                @p3                nvarchar(774),
  10919.                @p4                nvarchar(774),
  10920.                @p5                nvarchar(774),
  10921.                @p6                nvarchar(774))
  10922. as
  10923.     set nocount on
  10924.     declare @ret int
  10925.  
  10926.     create table #spfkeys (
  10927.         PKTABLE_QUALIFIER sysname     collate database_default null,
  10928.         PKTABLE_OWNER sysname    collate database_default null,
  10929.         PKTABLE_NAME sysname  collate database_default not null,
  10930.         PKCOLUMN_NAME sysname  collate database_default not null,
  10931.         FKTABLE_QUALIFIER sysname    collate database_default null,
  10932.         FKTABLE_OWNER sysname    collate database_default null,
  10933.         FKTABLE_NAME sysname  collate database_default not null,
  10934.         FKCOLUMN_NAME sysname  collate database_default not null,
  10935.         KEY_SEQ smallint not null,
  10936.         UPDATE_RULE smallint null,
  10937.         DELETE_RULE smallint null,
  10938.         FK_NAME sysname collate database_default null,
  10939.         PK_NAME sysname collate database_default null,
  10940.         DEFERRABILITY smallint null
  10941.         )
  10942.     insert into #spfkeys exec sp_fkeys @p1,@p2,@p3,@p4,@p5,@p6
  10943.     exec @ret = sp_cursoropen @handle output,
  10944.         'select * from #spfkeys',
  10945.         @scrollopt output, @ccopt output, @rows output
  10946.     drop table #spfkeys
  10947.     return @ret
  10948. go
  10949.  
  10950.  
  10951.  
  10952. if (charindex('8.00', @@version) = 0)
  10953. begin
  10954.     print ''
  10955.     print ''
  10956.     print 'Warning:'
  10957.     print 'you are installing the stored procedures '
  10958.     print 'on a pre 8.0 SQL Server.'
  10959.     print 'Ignore the following errors.'
  10960. end
  10961. go
  10962.  
  10963. create procedure sp_ddopen; 6(
  10964.                @handle            int output,
  10965.                @scrollopt        int output,
  10966.                @ccopt            int output,
  10967.                @rows            int output,
  10968.                @p1                nvarchar(774),
  10969.                @p2                nvarchar(774),
  10970.                @p3                nvarchar(774))
  10971. as
  10972.     set nocount on
  10973.     declare @ret int
  10974.  
  10975.     create table #sppkeys (
  10976.         TABLE_QUALIFIER sysname   collate database_default null,
  10977.         TABLE_OWNER sysname   collate database_default null,
  10978.         TABLE_NAME sysname    collate database_default not null,
  10979.         COLUMN_NAME sysname  collate database_default not null,
  10980.         KEY_SEQ smallint not null,
  10981.         PK_NAME sysname collate database_default null
  10982.         )
  10983.     insert into #sppkeys exec sp_pkeys @p1,@p2,@p3
  10984.     exec @ret = sp_cursoropen @handle output,
  10985.         'select * from #sppkeys',
  10986.         @scrollopt output, @ccopt output, @rows output
  10987.     drop table #sppkeys
  10988.     return @ret
  10989. go
  10990.  
  10991.  
  10992.  
  10993. if (charindex('8.00', @@version) = 0)
  10994. begin
  10995.     print ''
  10996.     print ''
  10997.     print 'Warning:'
  10998.     print 'you are installing the stored procedures '
  10999.     print 'on a pre 8.0 SQL Server.'
  11000.     print 'Ignore the following errors.'
  11001. end
  11002. go
  11003.  
  11004. create procedure sp_ddopen; 7(
  11005.                @handle            int output,
  11006.                @scrollopt        int output,
  11007.                @ccopt            int output,
  11008.                @rows            int output,
  11009.                @p1                nvarchar(774),
  11010.                @p2                nvarchar(774),
  11011.                @p3                nvarchar(774),
  11012.                @p4                nvarchar(774),
  11013.                @p5                nvarchar(774),
  11014.                @p6                nvarchar(774),
  11015.                @ODBCVer         int)
  11016. as
  11017.     set nocount on
  11018.     declare @ret int
  11019.  
  11020.     create table #spspeccol (
  11021.         SCOPE smallint null,
  11022.         COLUMN_NAME sysname collate database_default not null,
  11023.         DATA_TYPE smallint not null,
  11024.         TYPE_NAME sysname collate database_default not null,
  11025.         "PRECISION" int null,
  11026.         LENGTH int null,
  11027.         SCALE smallint null,
  11028.         PSEUDO_COLUMN smallint null
  11029.         )
  11030.     insert into #spspeccol exec sp_special_columns @p1,@p2,@p3,@p4,@p5,@p6,@ODBCVer
  11031.     exec @ret = sp_cursoropen @handle output,
  11032.         'select * from #spspeccol',
  11033.         @scrollopt output, @ccopt output, @rows output
  11034.     drop table #spspeccol
  11035.     return @ret
  11036. go
  11037.  
  11038.  
  11039.  
  11040. if (charindex('8.00', @@version) = 0)
  11041. begin
  11042.     print ''
  11043.     print ''
  11044.     print 'Warning:'
  11045.     print 'you are installing the stored procedures '
  11046.     print 'on a pre 8.0 SQL Server.'
  11047.     print 'Ignore the following errors.'
  11048. end
  11049. go
  11050.  
  11051. create procedure sp_ddopen; 8(
  11052.                @handle            int output,
  11053.                @scrollopt        int output,
  11054.                @ccopt            int output,
  11055.                @rows            int output,
  11056.                @p1                nvarchar(774),
  11057.                @p2                nvarchar(774),
  11058.                @p3                nvarchar(774),
  11059.                @p4                nvarchar(774),
  11060.                @ODBCVer         int)
  11061. as
  11062.     set nocount on
  11063.     declare @ret int
  11064.  
  11065.     create table #spproccol (
  11066.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  11067.         PROCEDURE_OWNER sysname  collate database_default null,
  11068.         PROCEDURE_NAME sysname collate database_default not null,
  11069.         COLUMN_NAME sysname collate database_default not null,
  11070.         COLUMN_TYPE smallint not null,
  11071.         DATA_TYPE smallint null,
  11072.         TYPE_NAME sysname collate database_default not null,
  11073.         "PRECISION" int null,
  11074.         LENGTH int null,
  11075.         SCALE smallint null,
  11076.         RADIX smallint null,
  11077.         NULLABLE smallint not null,
  11078.         REMARKS varchar(254) collate database_default null,
  11079.         COLUMN_DEF nvarchar(3000) collate database_default null,
  11080.         SQL_DATA_TYPE smallint null,
  11081.         SQL_DATETIME_SUB smallint null,
  11082.         CHAR_OCTET_LENGTH int null,
  11083.         ORDINAL_POSITION int not null,
  11084.         IS_NULLABLE varchar(254) collate database_default null,
  11085.         SS_DATA_TYPE tinyint null
  11086.         )
  11087.     insert into #spproccol exec sp_sproc_columns @p1,@p2,@p3,@p4,@ODBCVer
  11088.     exec @ret = sp_cursoropen @handle output,
  11089.         'select * from #spproccol',
  11090.         @scrollopt output, @ccopt output, @rows output
  11091.     drop table #spproccol
  11092.     return @ret
  11093. go
  11094.  
  11095.  
  11096.  
  11097. if (charindex('8.00', @@version) = 0)
  11098. begin
  11099.     print ''
  11100.     print ''
  11101.     print 'Warning:'
  11102.     print 'you are installing the stored procedures '
  11103.     print 'on a pre 8.0 SQL Server.'
  11104.     print 'Ignore the following errors.'
  11105. end
  11106. go
  11107.  
  11108. create procedure sp_ddopen; 9(
  11109.                @handle            int output,
  11110.                @scrollopt        int output,
  11111.                @ccopt            int output,
  11112.                @rows            int output,
  11113.                @p1                nvarchar(774),
  11114.                @p2                nvarchar(774),
  11115.                @p3                nvarchar(774),
  11116.                @p4                nvarchar(774),
  11117.                @p5                nvarchar(774),
  11118.                @p6                nvarchar(774))
  11119. as
  11120.     set nocount on
  11121.     declare @ret int
  11122.  
  11123.     create table #spstatistics (
  11124.         TABLE_QUALIFIER sysname   collate database_default null,
  11125.         TABLE_OWNER sysname   collate database_default null,
  11126.         TABLE_NAME sysname    collate database_default not null,
  11127.         NON_UNIQUE smallint null,
  11128.         INDEX_QUALIFIER sysname collate database_default null,
  11129.         INDEX_NAME sysname collate database_default null,
  11130.         TYPE smallint not null,
  11131.         SEQ_IN_INDEX smallint null,
  11132.         COLUMN_NAME sysname collate database_default null,
  11133.         COLLATION char(1) collate database_default null,
  11134.         CARDINALITY int null,
  11135.         PAGES int null,
  11136.         FILTER_CONDITION varchar(128) collate database_default null
  11137.         )
  11138.     insert into #spstatistics exec sp_statistics @p1,@p2,@p3,@p4,@p5,@p6
  11139.     exec @ret = sp_cursoropen @handle output,
  11140.         'select * from #spstatistics',
  11141.         @scrollopt output, @ccopt output, @rows output
  11142.     drop table #spstatistics
  11143.     return @ret
  11144. go
  11145.                
  11146.  
  11147.  
  11148. if (charindex('8.00', @@version) = 0)
  11149. begin
  11150.     print ''
  11151.     print ''
  11152.     print 'Warning:'
  11153.     print 'you are installing the stored procedures '
  11154.     print 'on a pre 8.0 SQL Server.'
  11155.     print 'Ignore the following errors.'
  11156. end
  11157. go
  11158.  
  11159. create procedure sp_ddopen; 10(
  11160.                @handle            int output,
  11161.                @scrollopt        int output,
  11162.                @ccopt            int output,
  11163.                @rows            int output,
  11164.                @p1                nvarchar(774),
  11165.                @p2                nvarchar(774),
  11166.                @p3                nvarchar(774))
  11167. as
  11168.     set nocount on
  11169.     declare @ret int
  11170.  
  11171.     create table #spprocedures (
  11172.         PROCEDURE_QUALIFIER sysname  collate database_default null,
  11173.         PROCEDURE_OWNER sysname  collate database_default null,
  11174.         PROCEDURE_NAME nvarchar(134) collate database_default not null, /*134=sysname+';'+ltrim(str(c.number,5))*/
  11175.         NUM_INPUT_PARAMS int null,
  11176.         NUM_OUTPUT_PARAMS int null,
  11177.         NUM_RESULT_SETS int null,
  11178.         REMARKS varchar(254) collate database_default null,
  11179.         PROCEDURE_TYPE smallint null
  11180.         )
  11181.     insert into #spprocedures exec sp_stored_procedures @p1,@p2,@p3
  11182.     exec @ret = sp_cursoropen @handle output,
  11183.         'select * from #spprocedures',
  11184.         @scrollopt output, @ccopt output, @rows output
  11185.     drop table #spprocedures
  11186.     return @ret
  11187. go
  11188.  
  11189.  
  11190.  
  11191. if (charindex('8.00', @@version) = 0)
  11192. begin
  11193.     print ''
  11194.     print ''
  11195.     print 'Warning:'
  11196.     print 'you are installing the stored procedures '
  11197.     print 'on a pre 8.0 SQL Server.'
  11198.     print 'Ignore the following errors.'
  11199. end
  11200. go
  11201.  
  11202. create procedure sp_ddopen; 11(
  11203.                @handle            int output,
  11204.                @scrollopt        int output,
  11205.                @ccopt            int output,
  11206.                @rows            int output,
  11207.                @p1                nvarchar(774),
  11208.                @p2                nvarchar(774),
  11209.                @p3                nvarchar(774))
  11210. as
  11211.     set nocount on
  11212.     declare @ret int
  11213.  
  11214.     create table #sptabpriv (
  11215.         TABLE_QUALIFIER sysname collate database_default null,
  11216.         TABLE_OWNER sysname collate database_default null,
  11217.         TABLE_NAME sysname collate database_default not null,
  11218.         GRANTOR sysname collate database_default null,
  11219.         GRANTEE sysname collate database_default not null,
  11220.         PRIVILEGE varchar(32) collate database_default not null,
  11221.         IS_GRANTABLE varchar(3) collate database_default null
  11222.         )
  11223.     insert into #sptabpriv exec sp_table_privileges @p1,@p2,@p3
  11224.     exec @ret = sp_cursoropen @handle output,
  11225.         'select * from #sptabpriv',
  11226.         @scrollopt output, @ccopt output, @rows output
  11227.     drop table #sptabpriv
  11228.     return @ret
  11229. go
  11230.  
  11231.  
  11232.  
  11233. if (charindex('8.00', @@version) = 0)
  11234. begin
  11235.     print ''
  11236.     print ''
  11237.     print 'Warning:'
  11238.     print 'you are installing the stored procedures '
  11239.     print 'on a pre 8.0 SQL Server.'
  11240.     print 'Ignore the following errors.'
  11241. end
  11242. go
  11243.  
  11244. create procedure sp_ddopen; 12(
  11245.                @handle            int output,
  11246.                @procname        sysname,
  11247.                @scrollopt        int output,
  11248.                @ccopt            int output,
  11249.                @rows            int output,
  11250.                @p1                nvarchar(774),
  11251.                @p2                nvarchar(774),
  11252.                @p3                nvarchar(774),
  11253.                @p4                nvarchar(774),
  11254.                @p5                nvarchar(774))
  11255. as
  11256.     set nocount on
  11257.     declare @ret int
  11258.  
  11259.     create table #sptables (
  11260.         TABLE_QUALIFIER sysname collate database_default null,
  11261.         TABLE_OWNER sysname collate database_default null,
  11262.         TABLE_NAME sysname collate database_default null,
  11263.         TABLE_TYPE    varchar(32) collate database_default null,
  11264.         REMARKS varchar(254) collate database_default null)
  11265.     if @procname = 'sp_tables'
  11266.     begin
  11267.         insert into #sptables exec sp_tables @p1,@p2,@p3,@p4
  11268.     end
  11269.     else
  11270.     begin
  11271.         insert into #sptables exec sp_tables_ex @p1,@p2,@p3,@p4,@p5
  11272.     end
  11273.     exec @ret = sp_cursoropen @handle output,
  11274.         'select * from #sptables',
  11275.         @scrollopt output, @ccopt output, @rows output
  11276.     drop table #sptables
  11277.     return @ret
  11278. go
  11279.  
  11280. create procedure sp_ddopen; 13(
  11281.                @handle          int output,
  11282.                @procname        sysname,
  11283.                @scrollopt       int output,
  11284.                @ccopt           int output,
  11285.                @rows            int output,
  11286.                @p1              nvarchar(774),  -- @table_name
  11287.                @p2              nvarchar(774),  -- @table_owner
  11288.                @p3              nvarchar(774),  -- @table_qualifier
  11289.                @p4              nvarchar(774))  -- @table_type
  11290. as
  11291.     -- this code was copied from sp_tableswc (for 8.0).  Any changes
  11292.     -- made here need to be evaluated for porting to sp_tableswc.
  11293.     set nocount on
  11294.     declare @ret int
  11295.     declare @databasename   sysname
  11296.     declare @qualprocname   nvarchar(141) /* 128 + '..sp_tables' */
  11297.  
  11298.     create table #sptables (
  11299.         TABLE_QUALIFIER sysname collate database_default null,
  11300.         TABLE_OWNER sysname collate database_default null,
  11301.         TABLE_NAME sysname collate database_default null,
  11302.         TABLE_TYPE  varchar(32) collate database_default null,
  11303.         REMARKS varchar(254) collate database_default null)
  11304.  
  11305.     declare databases CURSOR FOR
  11306.         select name from master..sysdatabases
  11307.         where name like @p3 and name <> 'model' and has_dbaccess(name)=1
  11308.         for read only
  11309.  
  11310.     open databases
  11311.     fetch next from databases into @databasename
  11312.     while (@@FETCH_STATUS <> -1)
  11313.     begin
  11314.         if (charindex('%', @databasename) = 0)
  11315.         begin   /* skip dbnames w/wildcard characters to prevent loop */
  11316.             select @qualprocname = @databasename + '..sp_tables'
  11317.             insert into #sptables exec @qualprocname
  11318.                 @p1, @p2, @databasename, @p4
  11319.         end
  11320.         fetch next from databases into @databasename
  11321.     end
  11322.     deallocate databases
  11323.     exec @ret = sp_cursoropen @handle output,
  11324.         'select * from #sptables',
  11325.         @scrollopt output, @ccopt output, @rows output
  11326.     drop table #sptables
  11327.     return @ret
  11328. go
  11329.  
  11330. grant execute on sp_ddopen to public
  11331. go
  11332.  
  11333. print 'creating sp_tableswc'
  11334. go
  11335.  
  11336. if (charindex('7.00', @@version) = 0 and
  11337.     charindex('8.00', @@version) = 0)
  11338. begin
  11339.     print ''
  11340.     print ''
  11341.     print 'Warning:'
  11342.     print 'you are installing the stored procedures '
  11343.     print 'on a pre 7.0 SQL Server.'
  11344.     print 'Ignore the following errors.'
  11345. end
  11346. go
  11347.  
  11348. /*    Procedure for 7.0 server */
  11349. create procedure sp_tableswc(
  11350.                @table_name        nvarchar(384)    = null,
  11351.                @table_owner     nvarchar(384)    = null,
  11352.                @table_qualifier sysname    = null,
  11353.                @table_type        varchar(100) = null)
  11354. as
  11355.     -- sp_ddopen; 13 contains the same code, so changes made here should
  11356.     -- be evaluated for porting to sp_ddopen; 13
  11357.     declare @databasename    sysname
  11358.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  11359.  
  11360.     create table #sptables (
  11361.         TABLE_QUALIFIER sysname null,
  11362.         TABLE_OWNER sysname null,
  11363.         TABLE_NAME sysname null,
  11364.         TABLE_TYPE    varchar(32) null,
  11365.         REMARKS varchar(254) null)
  11366.  
  11367.     declare databases CURSOR FOR
  11368.         select name from master..sysdatabases
  11369.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11370.         for read only
  11371.  
  11372.     open databases
  11373.     fetch next from databases into @databasename
  11374.     while (@@FETCH_STATUS <> -1)
  11375.     begin
  11376.         if (charindex('%', @databasename) = 0)
  11377.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  11378.             select @qualprocname = @databasename + '..sp_tables'
  11379.             insert into #sptables exec @qualprocname
  11380.                 @table_name, @table_owner, @databasename, @table_type
  11381.         end
  11382.         fetch next from databases into @databasename
  11383.     end
  11384.     deallocate databases
  11385.     select * from #sptables
  11386.         order by 4, 1, 2, 3
  11387. go
  11388.  
  11389. if (charindex('8.00', @@version) = 0)
  11390. begin
  11391.     print ''
  11392.     print ''
  11393.     print 'Warning:'
  11394.     print 'you are installing the stored procedures '
  11395.     print 'on a pre 8.0 SQL Server.'
  11396.     print 'Ignore the following errors.'
  11397. end
  11398. else
  11399. drop procedure sp_tableswc
  11400. go
  11401.  
  11402. /*    Procedure for 8.0 server */
  11403. create procedure sp_tableswc(
  11404.                @table_name        nvarchar(384)    = null,
  11405.                @table_owner     nvarchar(384)    = null,
  11406.                @table_qualifier sysname    = null,
  11407.                @table_type        varchar(100) = null)
  11408. as
  11409.     -- sp_ddopen; 13 contains the same code, so changes made here should
  11410.     -- be evaluated for porting to sp_ddopen; 13
  11411.     declare @databasename    sysname
  11412.     declare @qualprocname    nvarchar(141) /* 128 + '..sp_tables' */
  11413.  
  11414.     create table #sptables (
  11415.         TABLE_QUALIFIER sysname collate database_default null,
  11416.         TABLE_OWNER sysname collate database_default null,
  11417.         TABLE_NAME sysname collate database_default null,
  11418.         TABLE_TYPE    varchar(32) collate database_default null,
  11419.         REMARKS varchar(254) collate database_default null)
  11420.  
  11421.     declare databases CURSOR FOR
  11422.         select name from master..sysdatabases
  11423.         where name like @table_qualifier and name <> 'model' and has_dbaccess(name)=1
  11424.         for read only
  11425.  
  11426.     open databases
  11427.     fetch next from databases into @databasename
  11428.     while (@@FETCH_STATUS <> -1)
  11429.     begin
  11430.         if (charindex('%', @databasename) = 0)
  11431.         begin    /* skip dbnames w/wildcard characters to prevent loop */
  11432.             select @qualprocname = @databasename + '..sp_tables'
  11433.             insert into #sptables exec @qualprocname
  11434.                 @table_name, @table_owner, @databasename, @table_type
  11435.         end
  11436.         fetch next from databases into @databasename
  11437.     end
  11438.     deallocate databases
  11439.     select * from #sptables
  11440.         order by 4, 1, 2, 3
  11441. go
  11442.  
  11443. grant execute on sp_tableswc to public
  11444. go
  11445.  
  11446. dump tran master with no_log
  11447. go
  11448.  
  11449. /*-------------------------------------------------------------------------*/
  11450. /*-------------- CATALOG STORED PROCEDURES FOR SQLOLEDB  ------------------*/
  11451. /*-------------------------------------------------------------------------*/
  11452.  
  11453. print ''
  11454. print 'creating spt_provider_types'
  11455. go
  11456. if (charindex('7.00', @@version) = 0 and
  11457.     charindex('8.00', @@version) = 0)
  11458. begin /* Pre 8.0 Server */
  11459. create table spt_provider_types
  11460.     (
  11461.     ss_dtype        tinyint     not null,
  11462.     fixlen            int         null,        /* datatype len for variable, else null */
  11463.     type_name        sysname        not null,
  11464.     oledb_data_type        smallint    not null,
  11465.     best_match        bit        not null,
  11466.     is_nullable        tinyint        null,
  11467.     case_sensitive        bit        not null,
  11468.     fixed_prec_scale    bit        not null,
  11469.     is_long            bit        not null,
  11470.     auto_unique_value    tinyint        not null,
  11471.     data_precision        int         null,
  11472.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  11473.     column_size        int         null,
  11474.     literal_prefix        varchar(32)    null,
  11475.     literal_suffix        varchar(32)     null,
  11476.     searchable        int        not null,
  11477.     unsigned_attribute    tinyint        null,
  11478.     local_type_name     sysname        null
  11479.     )
  11480. print ''
  11481. print ''
  11482. print 'Warning:'
  11483. print 'you are installing the stored procedures '
  11484. print 'on a pre 8.0 SQL Server.'
  11485. print 'Ignore the following errors.'
  11486. end
  11487. go
  11488. if (charindex('7.00', @@version) > 0 or
  11489.     charindex('8.00', @@version) > 0)
  11490. begin /* 8.0 Server */
  11491. create table spt_provider_types
  11492.     (
  11493.     ss_dtype        tinyint     not null,
  11494.     fixlen            int         null,        /* datatype len for variable, else null */
  11495.     type_name        sysname        not null,
  11496.     oledb_data_type        smallint    not null,
  11497.     best_match        bit        not null,
  11498.     is_nullable        bit        null,
  11499.     case_sensitive        bit        not null,
  11500.     fixed_prec_scale    bit        not null,
  11501.     is_long            bit        not null,
  11502.     auto_unique_value    tinyint        not null,
  11503.     data_precision        int         null,
  11504.     numeric_scale        smallint    null,        /* min scale if 6.0 */
  11505.     column_size        int         null,
  11506.     literal_prefix        nvarchar(32)    null,
  11507.     literal_suffix        nvarchar(32)     null,
  11508.     searchable        int        not null,
  11509.     unsigned_attribute    tinyint        null,
  11510.     local_type_name     sysname        null
  11511.     )
  11512. end
  11513. go
  11514.  
  11515. grant select on spt_provider_types to public
  11516. go
  11517. dump tran master with no_log
  11518. go
  11519.  
  11520. /*
  11521. ** Insert the spt_provider_types rows for DBTYTPE_STR data types
  11522. */
  11523. begin tran
  11524.  
  11525. /* Get case sensitivity */
  11526. declare @case_sensitive bit
  11527. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  11528. /* Local Char */
  11529. insert into spt_provider_types values
  11530.     (
  11531.     47 /*SQLCHARACTER*/,        /* ss_dtype */
  11532.     0,                /* fixlen */
  11533.     'char',                /* type_name */
  11534.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  11535.     0,                /* best_match */
  11536.     null,                /* is_nullable */
  11537.     @case_sensitive,        /* case_sensitive */
  11538.     0,                /* fixed_prec_scale */
  11539.     0,                /* is_long */
  11540.     0,                /* auto_unique_value */
  11541.     null,                /* data_precision */
  11542.     null,                /* numeric_scale */
  11543.     null,                /* column_size */
  11544.     '''',                /* literal_prefix */
  11545.     '''',                /* literal_suffix */
  11546.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11547.     null,                /* unsigned_attribute */
  11548.     'char'                /* local_type_name */
  11549.     )
  11550.  
  11551. /* Local Varchar */
  11552. insert into spt_provider_types values
  11553.     (
  11554.     39 /*SQLVARCHAR*/,        /* ss_dtype */
  11555.     null,                /* fixlen */
  11556.     'varchar',            /* type_name */
  11557.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  11558.     1,                /* best_match */
  11559.     null,                /* is_nullable */
  11560.     @case_sensitive,        /* case_sensitive */
  11561.     0,                /* fixed_prec_scale */
  11562.     0,                /* is_long */
  11563.     0,                /* auto_unique_value */
  11564.     null,                /* data_precision */
  11565.     null,                /* numeric_scale */
  11566.     null,                /* column_size */
  11567.     '''',                /* literal_prefix */
  11568.     '''',                /* literal_suffix */
  11569.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11570.     null,                /* unsigned_attribute */
  11571.     'varchar'            /* local_type_name */
  11572.     )
  11573.  
  11574. /* Local Text */
  11575. insert into spt_provider_types values
  11576.     (
  11577.     35  /*SQLTEXT*/,        /* ss_dtype */
  11578.     null,                /* fixlen */
  11579.     'text',                /* type_name */
  11580.     129 /*DBTYPE_STR*/,        /* oledb_data_type */
  11581.     0,                /* best_match */
  11582.     null,                /* is_nullable */
  11583.     @case_sensitive,        /* case_sensitive */
  11584.     0,                /* fixed_prec_scale */
  11585.     1,                /* is_long */
  11586.     0,                /* auto_unique_value */
  11587.     null,                /* data_precision */
  11588.     null,                /* numeric_scale */
  11589.     2147483647,            /* column_size */
  11590.     '''',                /* literal_prefix */
  11591.     '''',                /* literal_suffix */
  11592.     2 /*DB_LIKE_ONLY*/,        /* searchable */
  11593.     null,                /* unsigned_attribute */
  11594.     'text'                /* local_type_name */
  11595.     )
  11596. commit tran
  11597. go
  11598. dump tran master with no_log
  11599. go
  11600.  
  11601. /*
  11602. ** Insert the spt_provider_types rows for DBTYTPE_BYTES data types
  11603. */
  11604. begin tran
  11605. /* Local Binary */
  11606. insert into spt_provider_types values
  11607.     (
  11608.     45 /*SQLBINARY*/,        /* ss_dtype */
  11609.     0,                /* fixlen */
  11610.     'binary',            /* type_name */
  11611.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  11612.     0,                /* best_match */
  11613.     null,                /* is_nullable */
  11614.     0,                /* case_sensitive */
  11615.     0,                /* fixed_prec_scale */
  11616.     0,                /* is_long */
  11617.     0,                /* auto_unique_value */
  11618.     null,                /* data_precision */
  11619.     null,                /* numeric_scale */
  11620.     null,                /* column_size */
  11621.     '0x',                /* literal_prefix */
  11622.     null,                /* literal_suffix */
  11623.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11624.     null,                /* unsigned_attribute */
  11625.     'binary'            /* local_type_name */
  11626.     )
  11627.  
  11628. /* Local Varbinary */
  11629. insert into spt_provider_types values
  11630.     (
  11631.     37 /*SQLVARBINARY*/,        /* ss_dtype */
  11632.     null,                /* fixlen */
  11633.     'varbinary',            /* type_name */
  11634.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  11635.     1,                /* best_match */
  11636.     null,                /* is_nullable */
  11637.     0,                /* case_sensitive */
  11638.     0,                /* fixed_prec_scale */
  11639.     0,                /* is_long */
  11640.     0,                /* auto_unique_value */
  11641.     null,                /* data_precision */
  11642.     null,                /* numeric_scale */
  11643.     null,                /* column_size */
  11644.     '0x',                /* literal_prefix */
  11645.     null,                /* literal_suffix */
  11646.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11647.     null,                /* unsigned_attribute */
  11648.     'varbinary'            /* local_type_name */
  11649.     )
  11650.  
  11651. /* Local Image */
  11652. insert into spt_provider_types values
  11653.     (
  11654.     34 /*SQLIMAGE*/,        /* ss_dtype */
  11655.     null,                /* fixlen */
  11656.     'image',            /* type_name */
  11657.     128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  11658.     0,                /* best_match */
  11659.     null,                /* is_nullable */
  11660.     0,                /* case_sensitive */
  11661.     0,                /* fixed_prec_scale */
  11662.     1,                /* is_long */
  11663.     0,                /* auto_unique_value */
  11664.     null,                /* data_precision */
  11665.     null,                /* numeric_scale */
  11666.     2147483647,            /* column_size */
  11667.     '0x',                /* literal_prefix */
  11668.     null,                /* literal_suffix */
  11669.     1 /*DB_UNSEARCHABLE*/,        /* searchable */
  11670.     null,                /* unsigned_attribute */
  11671.     'image'                /* local_type_name */
  11672.     )
  11673.  
  11674. commit tran
  11675. go
  11676. dump tran master with no_log
  11677. go
  11678.  
  11679. /*
  11680. ** Insert the spt_provider_types rows for DBTYPE_DBTIMESTAMP data types
  11681. */
  11682. begin tran
  11683.  
  11684. /* Local Datetime */
  11685. insert into spt_provider_types values
  11686.     (
  11687.     61 /*SQLDATETIME*/,        /* ss_dtype */
  11688.     8,                /* fixlen */
  11689.     'datetime',            /* type_name */
  11690.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11691.     1,                /* best_match */
  11692.     null,                /* is_nullable */
  11693.     0,                /* case_sensitive */
  11694.     0,                /* fixed_prec_scale */
  11695.     0,                /* is_long */
  11696.     0,                /* auto_unique_value */
  11697.     23,                /* data_precision */
  11698.     null,                /* numeric_scale */
  11699.     null,                /* column_size */
  11700.     '''',                /* literal_prefix */
  11701.     '''',                /* literal_suffix */
  11702.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11703.     null,                /* unsigned_attribute */
  11704.     'datetime'            /* local_type_name */
  11705.     )
  11706.  
  11707. /* Local Smalldatetime */
  11708. insert into spt_provider_types values
  11709.     (
  11710.     58 /*SQLDATETIM4*/,        /* ss_dtype */
  11711.     4,                /* fixlen */
  11712.     'smalldatetime',        /* type_name */
  11713.     135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11714.     0,                /* best_match */
  11715.     null,                /* is_nullable */
  11716.     0,                /* case_sensitive */
  11717.     0,                /* fixed_prec_scale */
  11718.     0,                /* is_long */
  11719.     0,                /* auto_unique_value */
  11720.     16,                /* data_precision */
  11721.     null,                /* numeric_scale */
  11722.     null,                /* column_size */
  11723.     '''',                /* literal_prefix */
  11724.     '''',                /* literal_suffix */
  11725.     4 /*DB_SEARCHABLE*/,        /* searchable */
  11726.     null,                /* unsigned_attribute */
  11727.     'smalldatetime'            /* local_type_name */
  11728.     )
  11729.  
  11730. if (charindex('7.00', @@version) = 0 and
  11731.     charindex('8.00', @@version) = 0)
  11732.     begin    /*    Add nullable type for non-Sphinx server */
  11733.     
  11734.     /* Local Datetimn */
  11735.     insert into spt_provider_types values
  11736.         (
  11737.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11738.         4,                /* fixlen */
  11739.         'smalldatetime',        /* type_name */
  11740.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11741.         0,                /* best_match */
  11742.         null,                /* is_nullable */
  11743.         0,                /* case_sensitive */
  11744.         0,                /* fixed_prec_scale */
  11745.         0,                /* is_long */
  11746.         0,                /* auto_unique_value */
  11747.         16,                /* data_precision */
  11748.         null,                /* numeric_scale */
  11749.         null,                /* column_size */
  11750.         '''',                /* literal_prefix */
  11751.         '''',                /* literal_suffix */
  11752.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11753.         null,                /* unsigned_attribute */
  11754.         'smalldatetime'            /* local_type_name */
  11755.         )    
  11756.     insert into spt_provider_types values
  11757.         (
  11758.         111 /*SQLDATETIMN*/,        /* ss_dtype */
  11759.         8,                /* fixlen */
  11760.         'datetime',            /* type_name */
  11761.         135 /*DBTYPE_DBTIMESTAMP*/,    /* oledb_data_type */
  11762.         0,                /* best_match */
  11763.         null,                /* is_nullable */
  11764.         0,                /* case_sensitive */
  11765.         0,                /* fixed_prec_scale */
  11766.         0,                /* is_long */
  11767.         0,                /* auto_unique_value */
  11768.         23,                /* data_precision */
  11769.         null,                /* numeric_scale */
  11770.         null,                /* column_size */
  11771.         '''',                /* literal_prefix */
  11772.         '''',                /* literal_suffix */
  11773.         4 /*DB_SEARCHABLE*/,        /* searchable */
  11774.         null,                /* unsigned_attribute */
  11775.         'datetime'            /* local_type_name */
  11776.         )
  11777.     end
  11778.  
  11779. commit tran
  11780. go
  11781. dump tran master with no_log
  11782. go
  11783.  
  11784. /*
  11785. ** Insert the spt_provider_types rows for DBTYPE_CY data types
  11786. */
  11787. begin tran
  11788.  
  11789. /* Local Smallmoney */
  11790. insert into spt_provider_types values
  11791.     (
  11792.     122 /*SQLMONEY4*/,        /* ss_dtype */
  11793.     4,                /* fixlen */
  11794.     'smallmoney',            /* type_name */
  11795.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11796.     0,                /* best_match */
  11797.     null,                /* is_nullable */
  11798.     0,                /* case_sensitive */
  11799.     1,                /* fixed_prec_scale */
  11800.     0,                /* is_long */
  11801.     0,                /* auto_unique_value */
  11802.     10,                /* data_precision */
  11803.     null,                /* numeric_scale */
  11804.     null,                /* column_size */
  11805.     '$',                /* literal_prefix */
  11806.     null,                /* literal_suffix */
  11807.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11808.     0,                /* unsigned_attribute */
  11809.     'smallmoney'            /* local_type_name */
  11810.     )    
  11811.     
  11812. /* Local Money */
  11813. insert into spt_provider_types values
  11814.     (
  11815.     60 /*SQLMONEY*/,        /* ss_dtype */
  11816.     8,                /* fixlen */
  11817.     'money',            /* type_name */
  11818.     6 /*DBTYPE_CY*/,        /* oledb_data_type */
  11819.     1,                /* best_match */
  11820.     null,                /* is_nullable */
  11821.     0,                /* case_sensitive */
  11822.     1,                /* fixed_prec_scale */
  11823.     0,                /* is_long */
  11824.     0,                /* auto_unique_value */
  11825.     19,                /* data_precision */
  11826.     null,                /* numeric_scale */
  11827.     null,                /* column_size */
  11828.     '$',                /* literal_prefix */
  11829.     null,                /* literal_suffix */
  11830.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11831.     0,                /* unsigned_attribute */
  11832.     'money'                /* local_type_name */
  11833.     )    
  11834.  
  11835. if (charindex('7.00', @@version) = 0 and
  11836.     charindex('8.00', @@version) = 0)
  11837.     begin    /*    Add nullable type for non-Sphinx server */
  11838.  
  11839.     /* Local Moneyn */
  11840.     insert into spt_provider_types values
  11841.         (
  11842.         110 /*SQLMONEYN*/,    /* ss_dtype */
  11843.         4,            /* fixlen */
  11844.         'smallmoney',        /* type_name */
  11845.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11846.         0,            /* best_match */
  11847.         null,            /* is_nullable */
  11848.         0,            /* case_sensitive */
  11849.         1,            /* fixed_prec_scale */
  11850.         0,            /* is_long */
  11851.         0,            /* auto_unique_value */
  11852.         10,            /* data_precision */
  11853.         null,            /* numeric_scale */
  11854.         null,            /* column_size */
  11855.         '$',            /* literal_prefix */
  11856.         null,            /* literal_suffix */
  11857.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11858.         0,            /* unsigned_attribute */
  11859.         'smallmoney'        /* local_type_name */
  11860.         )    
  11861.     insert into spt_provider_types values
  11862.         (
  11863.         110 /*SQLMONEYN*/,    /* ss_dtype */
  11864.         8,            /* fixlen */
  11865.         'money',        /* type_name */
  11866.         6 /*DBTYPE_CY*/,    /* oledb_data_type */
  11867.         0,            /* best_match */
  11868.         null,            /* is_nullable */
  11869.         0,            /* case_sensitive */
  11870.         1,            /* fixed_prec_scale */
  11871.         0,            /* is_long */
  11872.         0,            /* auto_unique_value */
  11873.         19,            /* data_precision */
  11874.         null,            /* numeric_scale */
  11875.         null,            /* column_size */
  11876.         '$',            /* literal_prefix */
  11877.         null,            /* literal_suffix */
  11878.         3 /*DB_ALL_EXCEPT_LIKE*/, /* searchable */
  11879.         null,            /* unsigned_attribute */
  11880.         'money'            /* local_type_name */
  11881.         )    
  11882.     end
  11883.  
  11884. commit tran
  11885. go
  11886. dump tran master with no_log
  11887. go
  11888.  
  11889. /*
  11890. ** Insert the spt_provider_types rows for the numeric data types
  11891. */
  11892. begin tran
  11893.  
  11894. /* Local Float */
  11895. insert into spt_provider_types values
  11896.     (
  11897.     62 /*SQLFLT8*/,            /* ss_dtype */
  11898.     8,                /* fixlen */
  11899.     'float',            /* type_name */
  11900.     5 /*DBTYPE_R8*/,        /* oledb_data_type */
  11901.     1,                /* best_match */
  11902.     null,                /* is_nullable */
  11903.     0,                /* case_sensitive */
  11904.     0,                /* fixed_prec_scale */
  11905.     0,                /* is_long */
  11906.     0,                /* auto_unique_value */
  11907.     15,                /* data_precision */
  11908.     null,                /* numeric_scale */
  11909.     null,                /* column_size */
  11910.     null,                /* literal_prefix */
  11911.     null,                /* literal_suffix */
  11912.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11913.     0,                /* unsigned_attribute */
  11914.     'float'                /* local_type_name */
  11915.     )    
  11916.  
  11917. /* Local Real */
  11918. insert into spt_provider_types values
  11919.     (
  11920.     59 /*SQLFLT4*/,            /* ss_dtype */
  11921.     4,                /* fixlen */
  11922.     'real',                /* type_name */
  11923.     4 /*DBTYPE_R4*/,        /* oledb_data_type */
  11924.     1,                /* best_match */
  11925.     null,                /* is_nullable */
  11926.     0,                /* case_sensitive */
  11927.     0,                /* fixed_prec_scale */
  11928.     0,                /* is_long */
  11929.     0,                /* auto_unique_value */
  11930.     7,                /* data_precision */
  11931.     null,                /* numeric_scale */
  11932.     null,                /* column_size */
  11933.     null,                /* literal_prefix */
  11934.     null,                /* literal_suffix */
  11935.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11936.     0,                /* unsigned_attribute */
  11937.     'real'                /* local_type_name */
  11938.     )    
  11939.  
  11940. /* Local Int */
  11941. insert into spt_provider_types values
  11942.     (
  11943.     56 /*SQLINT4*/,            /* ss_dtype */
  11944.     4,                /* fixlen */
  11945.     'int',                /* type_name */
  11946.     3 /*DBTYPE_I4*/,        /* oledb_data_type */
  11947.     1,                /* best_match */
  11948.     null,                /* is_nullable */
  11949.     0,                /* case_sensitive */
  11950.     1,                /* fixed_prec_scale */
  11951.     0,                /* is_long */
  11952.     1,                /* auto_unique_value */
  11953.     10,                /* data_precision */
  11954.     null,                /* numeric_scale */
  11955.     null,                /* column_size */
  11956.     null,                /* literal_prefix */
  11957.     null,                /* literal_suffix */
  11958.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11959.     0,                /* unsigned_attribute */
  11960.     'int'                /* local_type_name */
  11961.     )    
  11962.  
  11963. /* Local Smallint */
  11964. insert into spt_provider_types values
  11965.     (
  11966.     52 /*SQLINT2*/,            /* ss_dtype */
  11967.     2,                /* fixlen */
  11968.     'smallint',            /* type_name */
  11969.     2 /*DBTYPE_I2*/,        /* oledb_data_type */
  11970.     1,                /* best_match */
  11971.     null,                /* is_nullable */
  11972.     0,                /* case_sensitive */
  11973.     1,                /* fixed_prec_scale */
  11974.     0,                /* is_long */
  11975.     1,                /* auto_unique_value */
  11976.     5,                /* data_precision */
  11977.     null,                /* numeric_scale */
  11978.     null,                /* column_size */
  11979.     null,                /* literal_prefix */
  11980.     null,                /* literal_suffix */
  11981.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  11982.     0,                /* unsigned_attribute */
  11983.     'smallint'            /* local_type_name */
  11984.     )    
  11985.  
  11986. /* Local Tinyint */
  11987. insert into spt_provider_types values
  11988.     (
  11989.     48 /*SQLINT1*/,            /* ss_dtype */
  11990.     1,                /* fixlen */
  11991.     'tinyint',            /* type_name */
  11992.     17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  11993.     1,                /* best_match */
  11994.     null,                /* is_nullable */
  11995.     0,                /* case_sensitive */
  11996.     1,                /* fixed_prec_scale */
  11997.     0,                /* is_long */
  11998.     1,                /* auto_unique_value */
  11999.     3,                /* data_precision */
  12000.     null,                /* numeric_scale */
  12001.     null,                /* column_size */
  12002.     null,                /* literal_prefix */
  12003.     null,                /* literal_suffix */
  12004.     3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12005.     1,                /* unsigned_attribute */
  12006.     'tinyint'            /* local_type_name */
  12007.     )    
  12008. commit tran
  12009. go
  12010. dump tran master with no_log
  12011. go
  12012. begin tran    
  12013. if (charindex('6.00', @@version) > 0 or
  12014.     charindex('6.50', @@version) > 0 or
  12015.     charindex('7.00', @@version) > 0 or
  12016.     charindex('8.00', @@version) > 0)
  12017.     begin    /*    Add 6.0 data types */
  12018.     
  12019.     /* Local Decimal */
  12020.     insert into spt_provider_types values
  12021.         (
  12022.         55 /*SQLDECIMAL*/,        /* ss_dtype */
  12023.         0,                /* fixlen */
  12024.         'decimal',            /* type_name */
  12025.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  12026.         0,                /* best_match */
  12027.         null,                /* is_nullable */
  12028.         0,                /* case_sensitive */
  12029.         1,                /* fixed_prec_scale */
  12030.         0,                /* is_long */
  12031.         1,                /* auto_unique_value */
  12032.         38,                /* data_precision */
  12033.         null,                /* numeric_scale */
  12034.         null,                /* column_size */
  12035.         null,                /* literal_prefix */
  12036.         null,                /* literal_suffix */
  12037.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12038.         0,                /* unsigned_attribute */
  12039.         'decimal'            /* local_type_name */
  12040.         )    
  12041.  
  12042.     /* Local Numeric */
  12043.     insert into spt_provider_types values
  12044.         (
  12045.         63 /*SQLNUMERIC*/,        /* ss_dtype */
  12046.         0,                /* fixlen */
  12047.         'numeric',            /* type_name */
  12048.         131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  12049.         1,                /* best_match */
  12050.         null,                /* is_nullable */
  12051.         0,                /* case_sensitive */
  12052.         1,                /* fixed_prec_scale */
  12053.         0,                /* is_long */
  12054.         1,                /* auto_unique_value */
  12055.         38,                /* data_precision */
  12056.         null,                /* numeric_scale */
  12057.         null,                /* column_size */
  12058.         null,                /* literal_prefix */
  12059.         null,                /* literal_suffix */
  12060.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12061.         0,                /* unsigned_attribute */
  12062.         'numeric'            /* local_type_name */
  12063.         )    
  12064.     end
  12065. commit tran
  12066. go
  12067. dump tran master with no_log
  12068. go
  12069. begin tran
  12070. if (charindex('7.00', @@version) = 0 and
  12071.     charindex('8.00', @@version) = 0)
  12072.     begin    /*    Add nullable type for non-Sphinx server */
  12073.     
  12074.     /* Local Floatn */
  12075.     insert into spt_provider_types values
  12076.         (
  12077.         109 /*SQLFLTN*/,        /* ss_dtype */
  12078.         8,                /* fixlen */
  12079.         'float',            /* type_name */
  12080.         5 /*DBTYPE_R8*/,        /* oledb_data_type */
  12081.         0,                /* best_match */
  12082.         null,                /* is_nullable */
  12083.         0,                /* case_sensitive */
  12084.         0,                /* fixed_prec_scale */
  12085.         0,                /* is_long */
  12086.         0,                /* auto_unique_value */
  12087.         15,                /* data_precision */
  12088.         null,                /* numeric_scale */
  12089.         null,                /* column_size */
  12090.         null,                /* literal_prefix */
  12091.         null,                /* literal_suffix */
  12092.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12093.         null,                /* unsigned_attribute */
  12094.         'float'                /* local_type_name */
  12095.         )    
  12096.     insert into spt_provider_types values
  12097.         (
  12098.         109 /*SQLFLT4*/,        /* ss_dtype */
  12099.         4,                /* fixlen */
  12100.         'real',                /* type_name */
  12101.         4 /*DBTYPE_R4*/,        /* oledb_data_type */
  12102.         0,                /* best_match */
  12103.         null,                /* is_nullable */
  12104.         0,                /* case_sensitive */
  12105.         0,                /* fixed_prec_scale */
  12106.         0,                /* is_long */
  12107.         0,                /* auto_unique_value */
  12108.         7,                /* data_precision */
  12109.         null,                /* numeric_scale */
  12110.         null,                /* column_size */
  12111.         null,                /* literal_prefix */
  12112.         null,                /* literal_suffix */
  12113.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12114.         null,                /* unsigned_attribute */
  12115.         'real'                /* local_type_name */
  12116.         )    
  12117.  
  12118.     /* Local Intn */
  12119.     insert into spt_provider_types values
  12120.         (
  12121.         38 /*SQLINTN*/,            /* ss_dtype */
  12122.         4,                /* fixlen */
  12123.         'int',                /* type_name */
  12124.         3 /*DBTYPE_I4*/,        /* oledb_data_type */
  12125.         1,                /* best_match */
  12126.         null,                /* is_nullable */
  12127.         0,                /* case_sensitive */
  12128.         1,                /* fixed_prec_scale */
  12129.         0,                /* is_long */
  12130.         1,                /* auto_unique_value */
  12131.         10,                /* data_precision */
  12132.         null,                /* numeric_scale */
  12133.         null,                /* column_size */
  12134.         null,                /* literal_prefix */
  12135.         null,                /* literal_suffix */
  12136.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12137.         0,                /* unsigned_attribute */
  12138.         'int'                /* local_type_name */
  12139.         )    
  12140.     insert into spt_provider_types values
  12141.         (
  12142.         38 /*SQLINTN*/,            /* ss_dtype */
  12143.         2,                /* fixlen */
  12144.         'smallint',            /* type_name */
  12145.         2 /*DBTYPE_I2*/,        /* oledb_data_type */
  12146.         0,                /* best_match */
  12147.         null,                /* is_nullable */
  12148.         0,                /* case_sensitive */
  12149.         1,                /* fixed_prec_scale */
  12150.         0,                /* is_long */
  12151.         1,                /* auto_unique_value */
  12152.         5,                /* data_precision */
  12153.         null,                /* numeric_scale */
  12154.         null,                /* column_size */
  12155.         null,                /* literal_prefix */
  12156.         null,                /* literal_suffix */
  12157.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12158.         0,                /* unsigned_attribute */
  12159.         'smallint'            /* local_type_name */
  12160.         )    
  12161.     insert into spt_provider_types values
  12162.         (
  12163.         38 /*SQLINTN*/,            /* ss_dtype */
  12164.         1,                /* fixlen */
  12165.         'tinyint',            /* type_name */
  12166.         17 /*DBTYPE_UI1*/,        /* oledb_data_type */
  12167.         0,                /* best_match */
  12168.         null,                /* is_nullable */
  12169.         0,                /* case_sensitive */
  12170.         1,                /* fixed_prec_scale */
  12171.         0,                /* is_long */
  12172.         1,                /* auto_unique_value */
  12173.         3,                /* data_precision */
  12174.         null,                /* numeric_scale */
  12175.         null,                /* column_size */
  12176.         null,                /* literal_prefix */
  12177.         null,                /* literal_suffix */
  12178.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12179.         1,                /* unsigned_attribute */
  12180.         'tinyint'            /* local_type_name */
  12181.         )    
  12182.  
  12183.     if (charindex('6.00', @@version) > 0 or    charindex('6.50', @@version) > 0)
  12184.         begin    /*    Add 6.0 data types */
  12185.         
  12186.         /* Local Decimaln */
  12187.         insert into spt_provider_types values
  12188.             (
  12189.             106 /*SQLDECIMALN*/,        /* ss_dtype */
  12190.             0,                /* fixlen */
  12191.             'decimal',            /* type_name */
  12192.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  12193.             0,                /* best_match */
  12194.             null,                /* is_nullable */
  12195.             0,                /* case_sensitive */
  12196.             1,                /* fixed_prec_scale */
  12197.             0,                /* is_long */
  12198.             1,                /* auto_unique_value */
  12199.             38,                /* data_precision */
  12200.             null,                /* numeric_scale */
  12201.             null,                /* column_size */
  12202.             null,                /* literal_prefix */
  12203.             null,                /* literal_suffix */
  12204.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12205.             0,                /* unsigned_attribute */
  12206.             'decimal'            /* local_type_name */
  12207.             )    
  12208.  
  12209.         /* Local Numericn */
  12210.         insert into spt_provider_types values
  12211.             (
  12212.             108 /*SQLNUMERICN*/,        /* ss_dtype */
  12213.             0,                /* fixlen */
  12214.             'numeric',            /* type_name */
  12215.             131 /*DBTYPE_NUMERIC*/,        /* oledb_data_type */
  12216.             0,                /* best_match */
  12217.             null,                /* is_nullable */
  12218.             0,                /* case_sensitive */
  12219.             1,                /* fixed_prec_scale */
  12220.             0,                /* is_long */
  12221.             1,                /* auto_unique_value */
  12222.             38,                /* data_precision */
  12223.             null,                /* numeric_scale */
  12224.             null,                /* column_size */
  12225.             null,                /* literal_prefix */
  12226.             null,                /* literal_suffix */
  12227.             3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12228.             0,                /* unsigned_attribute */
  12229.             'numeric'            /* local_type_name */
  12230.             )    
  12231.         end
  12232.     end
  12233. commit tran
  12234. go
  12235. dump tran master with no_log
  12236. go
  12237.  
  12238. /*
  12239. **    Remaining data types
  12240. */
  12241. if (charindex('7.00', @@version) = 0 and
  12242.     charindex('8.00', @@version) = 0)
  12243. begin
  12244.     print ''
  12245.     print ''
  12246.     print 'Warning:'
  12247.     print 'you are installing the stored procedures '
  12248.     print 'on a pre 8.0 SQL Server.'
  12249.     print 'Ignore the following errors.'
  12250. end
  12251. go
  12252. begin tran
  12253.  
  12254. if (charindex('7.00', @@version) > 0 or
  12255.     charindex('8.00', @@version) > 0)
  12256.     begin
  12257.     /* Local Bit */
  12258.     insert into spt_provider_types values
  12259.         (
  12260.         50 /*SQLBIT*/,            /* ss_dtype */
  12261.         0,                /* fixlen */
  12262.         'bit',                /* type_name */
  12263.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  12264.         1,                /* best_match */
  12265.         null,                /* is_nullable */
  12266.         0,                /* case_sensitive */
  12267.         0,                /* fixed_prec_scale */
  12268.         0,                /* is_long */
  12269.         0,                /* auto_unique_value */
  12270.         1,                /* data_precision */
  12271.         null,                /* numeric_scale */
  12272.         null,                /* column_size */
  12273.         null,                /* literal_prefix */
  12274.         null,                /* literal_suffix */
  12275.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12276.         null,                /* unsigned_attribute */
  12277.         'bit'                /* local_type_name */
  12278.         )        
  12279.     
  12280.     /* Local Timestamp */
  12281.     insert into spt_provider_types values
  12282.         (
  12283.         0,                /* ss_dtype */
  12284.         8,                /* fixlen */
  12285.         'timestamp',            /* type_name */
  12286.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  12287.         0,                /* best_match */
  12288.         null,                /* is_nullable */
  12289.         0,                /* case_sensitive */
  12290.         0,                /* fixed_prec_scale */
  12291.         0,                /* is_long */
  12292.         0,                /* auto_unique_value */
  12293.         null,                /* data_precision */
  12294.         null,                /* numeric_scale */
  12295.         null,                /* column_size */
  12296.         '0x',                /* literal_prefix */
  12297.         null,                /* literal_suffix */
  12298.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12299.         null,                /* unsigned_attribute */
  12300.         'timestamp'            /* local_type_name */
  12301.         )    
  12302.     /* Local GUID */
  12303.     insert into spt_provider_types values
  12304.         (
  12305.         0,                /* ss_dtype */
  12306.         16,                /* fixlen */
  12307.         'uniqueidentifier',        /* type_name */
  12308.         72 /*DBTYPE_GUID*/,        /* oledb_data_type */
  12309.         1,                /* best_match */
  12310.         null,                /* is_nullable */
  12311.         0,                /* case_sensitive */
  12312.         0,                /* fixed_prec_scale */
  12313.         0,                /* is_long */
  12314.         0,                /* auto_unique_value */
  12315.         null,                /* data_precision */
  12316.         null,                /* numeric_scale */
  12317.         null,                /* column_size */
  12318.         '''',                /* literal_prefix */
  12319.         '''',                /* literal_suffix */
  12320.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12321.         null,                /* unsigned_attribute */
  12322.         'uniqueidentifier'        /* local_type_name */
  12323.         )    
  12324.     
  12325.     /* Get case sensitivity */
  12326.     declare @ncase_sensitive bit
  12327.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12328.  
  12329.     /* Local NChar */
  12330.     insert into spt_provider_types values
  12331.         (
  12332.         0,                /* ss_dtype */
  12333.         0,                /* fixlen */
  12334.         'nchar',            /* type_name */
  12335.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12336.         0,                /* best_match */
  12337.         null,                /* is_nullable */
  12338.         @ncase_sensitive,        /* case_sensitive */
  12339.         0,                /* fixed_prec_scale */
  12340.         0,                /* is_long */
  12341.         0,                /* auto_unique_value */
  12342.         null,                /* data_precision */
  12343.         null,                /* numeric_scale */
  12344.         null,                /* column_size */
  12345.         'N''',                /* literal_prefix */
  12346.         '''',                /* literal_suffix */
  12347.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12348.         null,                /* unsigned_attribute */
  12349.         'nchar'                /* local_type_name */
  12350.         )    
  12351.  
  12352.     /* Local NVarChar */
  12353.     insert into spt_provider_types values
  12354.         (
  12355.         0,                /* ss_dtype */
  12356.         null,                /* fixlen */
  12357.         'nvarchar',            /* type_name */
  12358.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12359.         1,                /* best_match */
  12360.         null,                /* is_nullable */
  12361.         @ncase_sensitive,        /* case_sensitive */
  12362.         0,                /* fixed_prec_scale */
  12363.         0,                /* is_long */
  12364.         0,                /* auto_unique_value */
  12365.         null,                /* data_precision */
  12366.         null,                /* numeric_scale */
  12367.         null,                /* column_size */
  12368.         'N''',                /* literal_prefix */
  12369.         '''',                /* literal_suffix */
  12370.         4 /*DB_SEARCHABLE*/,        /* searchable */
  12371.         null,                /* unsigned_attribute */
  12372.         'nvarchar'            /* local_type_name */
  12373.         )    
  12374.  
  12375.     /* Local NText */
  12376.     insert into spt_provider_types values
  12377.         (
  12378.         0,                /* ss_dtype */
  12379.         null,                /* fixlen */
  12380.         'ntext',            /* type_name */
  12381.         130 /*DBTYPE_WSTR*/,        /* oledb_data_type */
  12382.         0,                /* best_match */
  12383.         null,                /* is_nullable */
  12384.         @ncase_sensitive,        /* case_sensitive */
  12385.         0,                /* fixed_prec_scale */
  12386.         1,                /* is_long */
  12387.         0,                /* auto_unique_value */
  12388.         null,                /* data_precision */
  12389.         null,                /* numeric_scale */
  12390.         1073741823,            /* column_size */
  12391.         'N''',                /* literal_prefix */
  12392.         '''',                /* literal_suffix */
  12393.         2 /*DB_LIKE_ONLY*/,        /* searchable */
  12394.         null,                /* unsigned_attribute */
  12395.         'ntext'                /* local_type_name */
  12396.         )    
  12397.  
  12398.     if (charindex('8.00', @@version) > 0)
  12399.     begin
  12400.     /* Local BIGINT */
  12401.     insert into spt_provider_types values
  12402.         (
  12403.         127,                /* ss_dtype */
  12404.         8,                /* fixlen */
  12405.         'bigint',            /* type_name */
  12406.         20 /*DBTYPE_I8*/,        /* oledb_data_type */
  12407.         1,                /* best_match */
  12408.         null,                /* is_nullable */
  12409.         0,                /* case_sensitive */
  12410.         1,                /* fixed_prec_scale */
  12411.         0,                /* is_long */
  12412.         1,                /* auto_unique_value */
  12413.         19,                /* data_precision */
  12414.         null,            /* numeric_scale */
  12415.         null,            /* column_size */
  12416.         null,            /* literal_prefix */
  12417.         null,            /* literal_suffix */
  12418.         3 /*DB_ALL_EXCEPT_LIKE*/,        /* searchable */
  12419.         0,                /* unsigned_attribute */
  12420.         'bigint'        /* local_type_name */
  12421.         )
  12422.  
  12423.     /* sql_variant */
  12424.     insert into spt_provider_types values
  12425.         (
  12426.         98,                /* ss_dtype */
  12427.         null,            /* fixlen */
  12428.         'sql_variant',    /* type_name */
  12429.         12 /*DBTYPE_VARIANT*/,        /* oledb_data_type */
  12430.         1,                /* best_match */
  12431.         null,            /* is_nullable */
  12432.         0,                /* case_sensitive */
  12433.         0,                /* fixed_prec_scale */
  12434.         0,                /* is_long */
  12435.         0,                /* auto_unique_value */
  12436.         16,                /* data_precision sizeof(VARIANT)*/
  12437.         null,            /* numeric_scale */
  12438.         null,            /* column_size */
  12439.         null,            /* literal_prefix */
  12440.         null,            /* literal_suffix */
  12441.         3 ,                    /*DB_ALL_EXCEPT_LIKE*/
  12442.         null,            /* unsigned_attribute */
  12443.         'sql_variant'    /* local_type_name */
  12444.         )
  12445.     end
  12446.  
  12447.  
  12448.     /* data types changed in Sphinx: DO AFTER ALL TYPES INSERTED! */    
  12449.     update spt_provider_types set ss_dtype = (select xtype from systypes    where type_name like name+'%')
  12450.  
  12451.     end
  12452. commit tran
  12453. go
  12454. dump tran master with no_log
  12455. go
  12456. begin tran
  12457. if (charindex('7.00', @@version) = 0 and
  12458.     charindex('8.00', @@version) = 0)
  12459.     begin
  12460.     /* Local Bit */
  12461.     insert into spt_provider_types values
  12462.         (
  12463.         50 /*SQLBIT*/,            /* ss_dtype */
  12464.         0,                /* fixlen */
  12465.         'bit',                /* type_name */
  12466.         11 /*DBTYPE_BOOL*/,        /* oledb_data_type */
  12467.         1,                /* best_match */
  12468.         0,                /* is_nullable */
  12469.         0,                /* case_sensitive */
  12470.         0,                /* fixed_prec_scale */
  12471.         0,                /* is_long */
  12472.         0,                /* auto_unique_value */
  12473.         1,                /* data_precision */
  12474.         null,                /* numeric_scale */
  12475.         null,                /* column_size */
  12476.         null,                /* literal_prefix */
  12477.         null,                /* literal_suffix */
  12478.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12479.         null,                /* unsigned_attribute */
  12480.         'bit'                /* local_type_name */
  12481.         )    
  12482.  
  12483.     /* Local Timestamp */
  12484.     insert into spt_provider_types values
  12485.         (
  12486.         45 /*SQLBINARY*/,        /* ss_dtype */
  12487.         8,                /* fixlen */
  12488.         'timestamp',            /* type_name */
  12489.         128 /*DBTYPE_BYTES*/,        /* oledb_data_type */
  12490.         0,                /* best_match */
  12491.         null,                /* is_nullable */
  12492.         0,                /* case_sensitive */
  12493.         0,                /* fixed_prec_scale */
  12494.         0,                /* is_long */
  12495.         0,                /* auto_unique_value */
  12496.         null,                /* data_precision */
  12497.         null,                /* numeric_scale */
  12498.         null,                /* column_size */
  12499.         '0x',                /* literal_prefix */
  12500.         null,                /* literal_suffix */
  12501.         3 /*DB_ALL_EXCEPT_LIKE*/,    /* searchable */
  12502.         null,                /* unsigned_attribute */
  12503.         'timestamp'            /* local_type_name */
  12504.         )        
  12505. end
  12506.  
  12507. commit tran
  12508. go
  12509. dump tran master with no_log
  12510. go
  12511.  
  12512. create unique clustered index datatypeinfoclust on spt_provider_types(ss_dtype,fixlen)
  12513. go
  12514.  
  12515. dump tran master with no_log
  12516. go
  12517. if (charindex('6.00', @@version) > 0)
  12518.     begin
  12519.     if (exists (select * from sysobjects
  12520.         where name = 'spt_provider_types' and type = 'U '))
  12521.         begin
  12522.         drop table spt_provider_types
  12523.         dump tran master with no_log
  12524.         end
  12525.     end
  12526. go
  12527.  
  12528. if (charindex('7.00', @@version) = 0 and
  12529.     charindex('8.00', @@version) = 0)
  12530. begin
  12531.     print ''
  12532.     print ''
  12533.     print 'Warning:'
  12534.     print 'you are installing the stored procedures '
  12535.     print 'on a pre 8.0 SQL Server.'
  12536.     print 'Ignore the following errors.'
  12537. end
  12538. go
  12539.  
  12540.  
  12541. /* 
  12542. ** (2/11/98) 
  12543. ** The following SP is shared by instcat.sql and the server to add information
  12544. ** related to server language collations. The server calls it at the end of
  12545. ** collation change to update the catalog with the new language collations
  12546. ** Everything in this SP should NOT use tempdb.  
  12547. */
  12548.  
  12549. create procedure sp_add_server_sortinfo
  12550. as
  12551. -- spt_server_info has unigue clustered index on attribute_id
  12552. delete spt_server_info where attribute_id in (16,18)
  12553.  
  12554. --insert into spt_server_info
  12555. --    select 18,'COLLATION_SEQ',
  12556. --        'charset='+t2.name+' sort_order='+t1.name
  12557. --        +' charset_num='+rtrim(convert(char(4),t1.csid))+
  12558. --        ' sort_order_num='+rtrim(convert(char(4),t1.id))
  12559. --    from master.dbo.syscharsets t1, master.dbo.syscharsets t2, master.dbo.sysconfigures t3
  12560. --    where t1.csid=t2.id and t1.id=t3.value and t3.config in (123,1123)
  12561. declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12562. declare @case_sensitive bit
  12563. declare @ncase_sensitive bit
  12564. select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12565. select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12566. select @sortid = value from sysconfigures where config = 1123
  12567. select @csid = csid, @sortname = name from master.dbo.syscharsets where id = @sortid
  12568. select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12569.         + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12570.         ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12571.         from syscharsets where id = @csid
  12572.  
  12573. insert into spt_server_info
  12574.         values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12575.  
  12576. if @case_sensitive = 1 /* If case sensitive server */
  12577. begin
  12578.     insert into spt_server_info
  12579.         values (16,'IDENTIFIER_CASE','SENSITIVE')
  12580. end
  12581. else
  12582. begin
  12583.     insert into spt_server_info
  12584.         values (16,'IDENTIFIER_CASE','MIXED')
  12585. end
  12586.  
  12587.  
  12588. update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12589.     where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12590. update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12591.     where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12592. update spt_provider_types set case_sensitive = @case_sensitive 
  12593.     where oledb_data_type = 129 /*DBTYPE_STR*/
  12594. update spt_provider_types set case_sensitive = @ncase_sensitive
  12595.     where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12596. /* sp_add_server_sortinfo */
  12597. go  
  12598.  
  12599.  
  12600. if (charindex('7.00', @@version) > 0)
  12601. begin
  12602.     exec sp_add_server_sortinfo
  12603. end
  12604. go
  12605.  
  12606. -- 8.0 version of sp_add_server_sortinfo
  12607. --
  12608. create procedure sp_add_server_sortinfo75
  12609. as
  12610. -- spt_server_info has unigue clustered index on attribute_id
  12611.     delete spt_server_info where attribute_id in (16, 18)
  12612.  
  12613.     declare @sortid int, @csid int, @sortname varchar(255), @attribute_value varchar(255)
  12614.     declare @case_sensitive bit
  12615.     declare @ncase_sensitive bit
  12616.     select @case_sensitive = case when 'a' <> 'A' then 1 else 0 end
  12617.     select @ncase_sensitive = case when N'a' <> N'A' then 1 else 0 end
  12618.  
  12619.     select @sortid = convert(int, ServerProperty('sqlsortorder')) 
  12620.     select @csid = convert(int, ServerProperty('sqlcharset')) 
  12621.     select @sortname = name from master.dbo.syscharsets where id = @sortid
  12622.  
  12623.     if @sortid = 0 -- Non-SQL Collations
  12624.         BEGIN
  12625.         select @attribute_value = 'charset=' 
  12626.             + name
  12627.             + ' collation=' + isnull(convert(sysname, ServerProperty('collation')), ' ')
  12628.             from syscharsets where id = @csid
  12629.         END
  12630.     else
  12631.         BEGIN
  12632.         select @attribute_value = 'charset='+ name +' sort_order=' + @sortname
  12633.             + ' charset_num=' + rtrim(convert(char(4),@csid))+
  12634.             ' sort_order_num=' + rtrim(convert(char(4),@sortid))
  12635.             from syscharsets where id = @csid
  12636.         END
  12637.  
  12638.     insert into spt_server_info
  12639.             values (18,'COLLATION_SEQ', isnull(@attribute_value, ' '))
  12640.  
  12641.     if @case_sensitive = 1 /* If case sensitive server */
  12642.     begin
  12643.         insert into spt_server_info
  12644.             values (16,'IDENTIFIER_CASE','SENSITIVE')
  12645.     end
  12646.     else
  12647.     begin
  12648.         insert into spt_server_info
  12649.             values (16,'IDENTIFIER_CASE','MIXED')
  12650.     end
  12651.  
  12652.  
  12653.     update spt_datatype_info set CASE_SENSITIVE = @case_sensitive 
  12654.         where DATA_TYPE in (-1, 1, 12) /* CHAR types */
  12655.     update spt_datatype_info set CASE_SENSITIVE = @ncase_sensitive
  12656.         where DATA_TYPE in (-10, -9, -8) /* NCHAR types */
  12657.     update spt_provider_types set case_sensitive = @case_sensitive 
  12658.         where oledb_data_type = 129 /*DBTYPE_STR*/
  12659.     update spt_provider_types set case_sensitive = @ncase_sensitive
  12660.         where oledb_data_type = 130 /*DBTYPE_WSTR*/
  12661. /* sp_add_server_info75 */
  12662. go 
  12663.  
  12664.  
  12665.  
  12666. if (charindex('8.00', @@version) > 0)
  12667. begin
  12668.     exec sp_add_server_sortinfo75
  12669. end
  12670. go
  12671.  
  12672. print ''
  12673. print 'creating sp_catalogs_rowset'
  12674. go
  12675.  
  12676. create procedure sp_catalogs_rowset
  12677.     (
  12678.     @catalog_name        varchar(255)
  12679.     )        
  12680. as
  12681.     select
  12682.         CATALOG_NAME    = name,
  12683.         DESCRIPTION    = convert(varchar(1),null)
  12684.     from     master.dbo.sysdatabases
  12685.     where    name = @catalog_name
  12686. go
  12687. dump tran master with no_log
  12688. go
  12689. create procedure sp_catalogs_rowset;2
  12690.     (
  12691.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  12692.     )
  12693. as
  12694.     select
  12695.         CATALOG_NAME    = name,
  12696.         DESCRIPTION    = convert(varchar(1),null)
  12697.     from     master.dbo.sysdatabases
  12698.     order by 1
  12699. go
  12700. dump tran master with no_log
  12701. go
  12702.  
  12703. if (charindex('7.00', @@version) = 0 and
  12704.     charindex('8.00', @@version) = 0)
  12705. begin
  12706.     print ''
  12707.     print ''
  12708.     print 'Warning:'
  12709.     print 'you are installing the stored procedures '
  12710.     print 'on a pre 8.0 SQL Server.'
  12711.     print 'Ignore the following errors.'
  12712. end
  12713. else
  12714.     drop proc sp_catalogs_rowset
  12715. go
  12716.  
  12717. /*    Procedure for 8.0 server */
  12718. create procedure sp_catalogs_rowset
  12719.     (
  12720.     @catalog_name        sysname
  12721.     )        
  12722. as
  12723.     select
  12724.         CATALOG_NAME    = name,
  12725.         DESCRIPTION    = convert(nvarchar(1),null)
  12726.     from     master.dbo.sysdatabases
  12727.     where    name = @catalog_name
  12728. go
  12729. dump tran master with no_log
  12730. go
  12731. create procedure sp_catalogs_rowset;2
  12732.     (
  12733.     @dummy        int /* remove when Hydra bug 17032 is fixed */
  12734.     )
  12735. as
  12736.     select
  12737.         CATALOG_NAME    = name,
  12738.         DESCRIPTION    = convert(nvarchar(1),null)
  12739.     from     master.dbo.sysdatabases
  12740.     order by 1
  12741. go
  12742. dump tran master with no_log
  12743. go
  12744. create procedure sp_catalogs_rowset;5
  12745.     (
  12746.     @server_name    sysname,
  12747.     @catalog_name    sysname = NULL
  12748.     )
  12749. as
  12750.     select    CATALOG_NAME,
  12751.         DESCRIPTION
  12752.     from master.dbo.SYSREMOTE_CATALOGS < @server_name, @catalog_name >
  12753.     order by 1
  12754. go
  12755.  
  12756. grant execute on sp_catalogs_rowset to public
  12757. go
  12758.  
  12759. dump tran master with no_log
  12760. go
  12761. if (charindex('6.00', @@version) > 0)
  12762.     begin
  12763.     if (exists (select * from sysobjects
  12764.             where name = 'sp_catalogs_rowset' and type = 'P '))
  12765.         begin
  12766.         drop procedure sp_catalogs_rowset
  12767.         dump tran master with no_log
  12768.         end
  12769.     end
  12770. go
  12771.  
  12772.  
  12773. print ''
  12774. print 'creating sp_column_privileges_rowset'
  12775. go
  12776.  
  12777. /*    Procedure for 6.0 and 6.5 server */
  12778. CREATE PROCEDURE sp_column_privileges_rowset
  12779.     (
  12780.     @table_name     varchar(255) = null,
  12781.     @table_schema    varchar(255) = null,
  12782.     @column_name    varchar(255) = null,
  12783.     @grantor    varchar(255) = null,
  12784.     @grantee    varchar(255) = null
  12785.     )
  12786. as
  12787. IF @table_name is not null
  12788.     BEGIN
  12789.     select
  12790.         GRANTOR        = user_name(p.grantor),
  12791.         GRANTEE        = user_name(u.uid),
  12792.         TABLE_CATALOG    = db_name(),
  12793.         TABLE_SCHEMA    = user_name(o.uid),
  12794.         TABLE_NAME    = o.name,
  12795.         COLUMN_NAME    = c.name,
  12796.         COLUMN_GUID    = convert(binary(16),null),
  12797.         COLUMN_PROPID    = convert(int,null),
  12798.         PRIVILEGE_TYPE    = convert(varchar(30),
  12799.                     case p.action
  12800.                     when 193 then 'SELECT'
  12801.                     when 195 then 'INSERT'
  12802.                     when 197 then 'UPDATE'
  12803.                     else 'REFERENCES'
  12804.                     end),
  12805.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12806.     from 
  12807.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12808.     where
  12809.         o.name = @table_name
  12810.     and     o.type in ('U','V','S')
  12811.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12812.     and    c.id = o.id
  12813.     and     (@column_name is null or @column_name = c.name)
  12814.     and     c.id = p.id
  12815.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12816.     and     case 
  12817.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12818.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12819.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12820.             end & v.high <> 0            /* permission applies to this column */
  12821.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12822.     and     v.type = 'P'
  12823.     and     v.number = c.colid
  12824.     and    (@grantee is null or @grantee = user_name(u.uid))
  12825.             /* expand groups */
  12826.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12827.     and     p.protecttype <> 206    /* only grant rows */
  12828.     and     p.action in (26,193,195,197)
  12829.     and     o.uid <> u.uid            /* no rows for owner */
  12830.     and     not exists (            /* exclude revoke'd privileges */
  12831.             select *
  12832.             from sysprotects p1
  12833.             where
  12834.                 p1.protecttype = 206
  12835.             and     p1.action = p.action
  12836.             and     p1.id = p.id
  12837.             and     p1.uid = u.uid
  12838.             and     case 
  12839.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12840.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12841.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12842.                 end & v.high <> 0)            /* permission applies to this column */
  12843.     union
  12844.     select    /*    Add rows for table owner */
  12845.         GRANTOR        = user_name(u.uid),
  12846.         GRANTEE        = user_name(o.uid),
  12847.         TABLE_CATALOG    = db_name(),
  12848.         TABLE_SCHEMA    = user_name(o.uid),
  12849.         TABLE_NAME    = o.name,
  12850.         COLUMN_NAME    = c.name,
  12851.         COLUMN_GUID    = convert(binary(16),null),
  12852.         COLUMN_PROPID    = convert(int,null),
  12853.         PRIVILEGE_TYPE    = convert(varchar(30),
  12854.                     case v.number
  12855.                     when 193 then 'SELECT'
  12856.                     when 195 then 'INSERT'
  12857.                     when 197 then 'UPDATE'
  12858.                     else 'REFERENCES'
  12859.                     end),
  12860.         IS_GRANTABLE    = convert(bit,1)    
  12861.     from 
  12862.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12863.     where
  12864.         o.name = @table_name
  12865.     and     o.type in ('U','V','S')
  12866.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12867.     and    (@grantee is null or @grantee = user_name(o.uid))
  12868.     and    c.id = o.id
  12869.     and     (@column_name is null or @column_name = c.name)
  12870.     and     u.uid = 1        /* grantor is dbo of database */
  12871.     and    (@grantor is null or @grantor = user_name(u.uid))
  12872.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12873.     and     v.number in (26,193,195,197)
  12874.     and     not exists (        /* exclude revoke'd privileges */
  12875.             select *
  12876.             from     sysprotects p1
  12877.             where    p1.protecttype = 206
  12878.             and     p1.action = v.number
  12879.             and     p1.id = o.id
  12880.             and     p1.uid = o.uid)
  12881.     order by 4, 5, 6, 9, 1, 2
  12882.     END
  12883. ELSE
  12884.     BEGIN
  12885.     select
  12886.         GRANTOR        = user_name(p.grantor),
  12887.         GRANTEE        = user_name(u.uid),
  12888.         TABLE_CATALOG    = db_name(),
  12889.         TABLE_SCHEMA    = user_name(o.uid),
  12890.         TABLE_NAME    = o.name,
  12891.         COLUMN_NAME    = c.name,
  12892.         COLUMN_GUID    = convert(binary(16),null),
  12893.         COLUMN_PROPID    = convert(int,null),
  12894.         PRIVILEGE_TYPE    = convert(varchar(30),
  12895.                     case p.action
  12896.                     when 193 then 'SELECT'
  12897.                     when 195 then 'INSERT'
  12898.                     when 197 then 'UPDATE'
  12899.                     else 'REFERENCES'
  12900.                     end),
  12901.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  12902.     from 
  12903.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  12904.     where
  12905.         o.type in ('U','V','S')
  12906.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12907.     and    c.id = o.id
  12908.     and     (@column_name is null or @column_name = c.name)
  12909.     and     c.id = p.id
  12910.     and    (@grantor is null or @grantor = user_name(p.grantor))
  12911.     and     case 
  12912.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12913.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  12914.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  12915.             end & v.high <> 0            /* permission applies to this column */
  12916.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  12917.     and     v.type = 'P'
  12918.     and     v.number = c.colid
  12919.     and    (@grantee is null or @grantee = user_name(u.uid))
  12920.             /* expand groups */
  12921.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  12922.     and     p.protecttype <> 206    /* only grant rows */
  12923.     and     p.action in (26,193,195,197)
  12924.     and     o.uid <> u.uid            /* no rows for owner */
  12925.     and     not exists (            /* exclude revoke'd privileges */
  12926.             select *
  12927.             from sysprotects p1
  12928.             where
  12929.                 p1.protecttype = 206
  12930.             and     p1.action = p.action
  12931.             and     p1.id = p.id
  12932.             and     p1.uid = u.uid
  12933.             and     case 
  12934.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  12935.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  12936.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  12937.                 end & v.high <> 0)            /* permission applies to this column */
  12938.     union
  12939.     select    /*    Add rows for table owner */
  12940.         GRANTOR        = user_name(u.uid),
  12941.         GRANTEE        = user_name(o.uid),
  12942.         TABLE_CATALOG    = db_name(),
  12943.         TABLE_SCHEMA    = user_name(o.uid),
  12944.         TABLE_NAME    = o.name,
  12945.         COLUMN_NAME    = c.name,
  12946.         COLUMN_GUID    = convert(binary(16),null),
  12947.         COLUMN_PROPID    = convert(int,null),
  12948.         PRIVILEGE_TYPE    = convert(varchar(30),
  12949.                     case v.number
  12950.                     when 193 then 'SELECT'
  12951.                     when 195 then 'INSERT'
  12952.                     when 197 then 'UPDATE'
  12953.                     else 'REFERENCES'
  12954.                     end),
  12955.         IS_GRANTABLE    = convert(bit,1)    
  12956.     from 
  12957.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  12958.     where
  12959.          o.type in ('U','V','S')
  12960.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  12961.     and    (@grantee is null or @grantee = user_name(o.uid))
  12962.     and    c.id = o.id
  12963.     and     (@column_name is null or @column_name = c.name)
  12964.     and     u.uid = 1        /* grantor is dbo of database */
  12965.     and    (@grantor is null or @grantor = user_name(u.uid))
  12966.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  12967.     and     v.number in (26,193,195,197)
  12968.     and     not exists (        /* exclude revoke'd privileges */
  12969.             select *
  12970.             from     sysprotects p1
  12971.             where    p1.protecttype = 206
  12972.             and     p1.action = v.number
  12973.             and     p1.id = o.id
  12974.             and     p1.uid = o.uid)
  12975.     order by 4, 5, 6, 9, 1, 2
  12976.     END
  12977. go
  12978. dump tran master with no_log
  12979. go
  12980. CREATE PROCEDURE sp_column_privileges_rowset;2
  12981.     (
  12982.        @handle        int output,
  12983.        @scrollopt    int output,
  12984.     @ccopt        int output,
  12985.     @rows        int output,
  12986.     @table_name     varchar(255) = null,
  12987.     @table_schema    varchar(255) = null,
  12988.     @column_name    varchar(255) = null,
  12989.     @grantor    varchar(255) = null,
  12990.     @grantee    varchar(255) = null
  12991.     )
  12992. as
  12993.  
  12994. declare @ret int
  12995.  
  12996. SET NOCOUNT ON
  12997.     
  12998. create table #spcprivsrowset1
  12999.     (
  13000.     GRANTOR        sysname not null,
  13001.     GRANTEE        sysname not null,
  13002.     TABLE_CATALOG    sysname not null,
  13003.     TABLE_SCHEMA    sysname not null,
  13004.     TABLE_NAME    sysname not null,
  13005.     COLUMN_NAME    sysname not null,
  13006.     COLUMN_GUID    binary(16) null,
  13007.     COLUMN_PROPID    int null,
  13008.     PRIVILEGE_TYPE    sysname not null,
  13009.     IS_GRANTABLE    bit not null
  13010.     )
  13011.  
  13012. IF @table_name is not null
  13013.     BEGIN
  13014.     insert into #spcprivsrowset1
  13015.     select
  13016.         GRANTOR        = user_name(p.grantor),
  13017.         GRANTEE        = user_name(u.uid),
  13018.         TABLE_CATALOG    = db_name(),
  13019.         TABLE_SCHEMA    = user_name(o.uid),
  13020.         TABLE_NAME    = o.name,
  13021.         COLUMN_NAME    = c.name,
  13022.         COLUMN_GUID    = convert(binary(16),null),
  13023.         COLUMN_PROPID    = convert(int,null),
  13024.         PRIVILEGE_TYPE    = convert(varchar(30),
  13025.                     case p.action
  13026.                     when 193 then 'SELECT'
  13027.                     when 195 then 'INSERT'
  13028.                     when 197 then 'UPDATE'
  13029.                     else 'REFERENCES'
  13030.                     end),
  13031.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13032.     from 
  13033.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  13034.     where
  13035.         o.name = @table_name
  13036.     and     o.type in ('U','V','S')
  13037.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13038.     and    c.id = o.id
  13039.     and     (@column_name is null or @column_name = c.name)
  13040.     and     c.id = p.id
  13041.     and    (@grantor is null or @grantor = user_name(p.grantor))
  13042.     and     case 
  13043.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13044.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13045.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13046.             end & v.high <> 0            /* permission applies to this column */
  13047.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  13048.     and     v.type = 'P'
  13049.     and     v.number = c.colid
  13050.     and    (@grantee is null or @grantee = user_name(u.uid))
  13051.             /* expand groups */
  13052.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  13053.     and     p.protecttype <> 206    /* only grant rows */
  13054.     and     p.action in (26,193,195,197)
  13055.     and     o.uid <> u.uid            /* no rows for owner */
  13056.     and     not exists (            /* exclude revoke'd privileges */
  13057.             select *
  13058.             from sysprotects p1
  13059.             where
  13060.                 p1.protecttype = 206
  13061.             and     p1.action = p.action
  13062.             and     p1.id = p.id
  13063.             and     p1.uid = u.uid
  13064.             and     case 
  13065.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13066.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13067.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13068.                 end & v.high <> 0)            /* permission applies to this column */
  13069.     union
  13070.     select    /*    Add rows for table owner */
  13071.         GRANTOR        = user_name(u.uid),
  13072.         GRANTEE        = user_name(o.uid),
  13073.         TABLE_CATALOG    = db_name(),
  13074.         TABLE_SCHEMA    = user_name(o.uid),
  13075.         TABLE_NAME    = o.name,
  13076.         COLUMN_NAME    = c.name,
  13077.         COLUMN_GUID    = convert(binary(16),null),
  13078.         COLUMN_PROPID    = convert(int,null),
  13079.         PRIVILEGE_TYPE    = convert(varchar(30),
  13080.                     case v.number
  13081.                     when 193 then 'SELECT'
  13082.                     when 195 then 'INSERT'
  13083.                     when 197 then 'UPDATE'
  13084.                     else 'REFERENCES'
  13085.                     end),
  13086.         IS_GRANTABLE    = convert(bit,1)    
  13087.     from 
  13088.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13089.     where
  13090.         o.name = @table_name
  13091.     and     o.type in ('U','V','S')
  13092.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13093.     and    (@grantee is null or @grantee = user_name(o.uid))
  13094.     and    c.id = o.id
  13095.     and     (@column_name is null or @column_name = c.name)
  13096.     and     u.uid = 1        /* grantor is dbo of database */
  13097.     and    (@grantor is null or @grantor = user_name(u.uid))
  13098.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13099.     and     v.number in (26,193,195,197)
  13100.     and     not exists (        /* exclude revoke'd privileges */
  13101.             select *
  13102.             from     sysprotects p1
  13103.             where    p1.protecttype = 206
  13104.             and     p1.action = v.number
  13105.             and     p1.id = o.id
  13106.             and     p1.uid = o.uid)
  13107.     order by 4, 5, 6, 9, 1, 2
  13108.     END
  13109. ELSE
  13110.     BEGIN
  13111.     insert into #spcprivsrowset1
  13112.     select
  13113.         GRANTOR        = user_name(p.grantor),
  13114.         GRANTEE        = user_name(u.uid),
  13115.         TABLE_CATALOG    = db_name(),
  13116.         TABLE_SCHEMA    = user_name(o.uid),
  13117.         TABLE_NAME    = o.name,
  13118.         COLUMN_NAME    = c.name,
  13119.         COLUMN_GUID    = convert(binary(16),null),
  13120.         COLUMN_PROPID    = convert(int,null),
  13121.         PRIVILEGE_TYPE    = convert(varchar(30),
  13122.                     case p.action
  13123.                     when 193 then 'SELECT'
  13124.                     when 195 then 'INSERT'
  13125.                     when 197 then 'UPDATE'
  13126.                     else 'REFERENCES'
  13127.                     end),
  13128.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13129.     from 
  13130.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c
  13131.     where
  13132.         o.type in ('U','V','S')
  13133.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13134.     and    c.id = o.id
  13135.     and     (@column_name is null or @column_name = c.name)
  13136.     and     c.id = p.id
  13137.     and    (@grantor is null or @grantor = user_name(p.grantor))
  13138.     and     case 
  13139.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13140.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13141.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13142.             end & v.high <> 0            /* permission applies to this column */
  13143.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  13144.     and     v.type = 'P'
  13145.     and     v.number = c.colid
  13146.     and    (@grantee is null or @grantee = user_name(u.uid))
  13147.             /* expand groups */
  13148.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  13149.     and     p.protecttype <> 206    /* only grant rows */
  13150.     and     p.action in (26,193,195,197)
  13151.     and     o.uid <> u.uid            /* no rows for owner */
  13152.     and     not exists (            /* exclude revoke'd privileges */
  13153.             select *
  13154.             from sysprotects p1
  13155.             where
  13156.                 p1.protecttype = 206
  13157.             and     p1.action = p.action
  13158.             and     p1.id = p.id
  13159.             and     p1.uid = u.uid
  13160.             and     case 
  13161.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13162.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13163.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13164.                 end & v.high <> 0)            /* permission applies to this column */
  13165.     union
  13166.     select    /*    Add rows for table owner */
  13167.         GRANTOR        = user_name(u.uid),
  13168.         GRANTEE        = user_name(o.uid),
  13169.         TABLE_CATALOG    = db_name(),
  13170.         TABLE_SCHEMA    = user_name(o.uid),
  13171.         TABLE_NAME    = o.name,
  13172.         COLUMN_NAME    = c.name,
  13173.         COLUMN_GUID    = convert(binary(16),null),
  13174.         COLUMN_PROPID    = convert(int,null),
  13175.         PRIVILEGE_TYPE    = convert(varchar(30),
  13176.                     case v.number
  13177.                     when 193 then 'SELECT'
  13178.                     when 195 then 'INSERT'
  13179.                     when 197 then 'UPDATE'
  13180.                     else 'REFERENCES'
  13181.                     end),
  13182.         IS_GRANTABLE    = convert(bit,1)    
  13183.     from 
  13184.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13185.     where
  13186.          o.type in ('U','V','S')
  13187.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13188.     and    (@grantee is null or @grantee = user_name(o.uid))
  13189.     and    c.id = o.id
  13190.     and     (@column_name is null or @column_name = c.name)
  13191.     and     u.uid = 1        /* grantor is dbo of database */
  13192.     and    (@grantor is null or @grantor = user_name(u.uid))
  13193.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13194.     and     v.number in (26,193,195,197)
  13195.     and     not exists (        /* exclude revoke'd privileges */
  13196.             select *
  13197.             from     sysprotects p1
  13198.             where    p1.protecttype = 206
  13199.             and     p1.action = v.number
  13200.             and     p1.id = o.id
  13201.             and     p1.uid = o.uid)
  13202.     order by 4, 5, 6, 9, 1, 2
  13203.     END
  13204.  
  13205. exec @ret = sp_cursoropen @handle output, 'select * from #spcprivsrowset1',
  13206.     @scrollopt output, @ccopt output, @rows output
  13207.  
  13208. drop table #spcprivsrowset1
  13209.  
  13210. return isnull(@ret,0)
  13211. go
  13212. dump tran master with no_log
  13213. go
  13214. CREATE PROCEDURE sp_column_privileges_rowset;3
  13215. as
  13216.     select
  13217.         GRANTOR        = convert(sysname,' '),
  13218.         GRANTEE        = convert(sysname,' '),
  13219.         TABLE_CATALOG    = convert(sysname,' '),
  13220.         TABLE_SCHEMA    = convert(sysname,' '),
  13221.         TABLE_NAME    = convert(sysname,' '),
  13222.         COLUMN_NAME    = convert(sysname,' '),
  13223.         COLUMN_GUID    = convert(binary(16),null),
  13224.         COLUMN_PROPID    = convert(int,null),
  13225.         PRIVILEGE_TYPE    = convert(varchar(30),' '),
  13226.         IS_GRANTABLE    = convert(bit,1)
  13227.     where    1=0
  13228. go
  13229. dump tran master with no_log
  13230. go
  13231.  
  13232. if (charindex('7.00', @@version) = 0 and
  13233.     charindex('8.00', @@version) = 0)
  13234. begin
  13235.     print ''
  13236.     print ''
  13237.     print 'Warning:'
  13238.     print 'you are installing the stored procedures '
  13239.     print 'on a pre 8.0 SQL Server.'
  13240.     print 'Ignore the following errors.'
  13241. end
  13242. else
  13243.     drop proc sp_column_privileges_rowset
  13244. go
  13245.  
  13246.  
  13247. /*    Procedure for 8.0 server */
  13248. CREATE PROCEDURE sp_column_privileges_rowset
  13249.     (
  13250.     @table_name         sysname,
  13251.     @table_schema        sysname = null,
  13252.     @column_name        sysname = null,
  13253.     @grantor        sysname = null,
  13254.     @grantee        sysname = null
  13255.     )
  13256. as
  13257.     select
  13258.         GRANTOR        = user_name(p.grantor),
  13259.         GRANTEE        = user_name(u.uid),
  13260.         TABLE_CATALOG    = db_name(),
  13261.         TABLE_SCHEMA    = user_name(o.uid),
  13262.         TABLE_NAME    = o.name,
  13263.         COLUMN_NAME    = c.name,
  13264.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13265.         COLUMN_PROPID    = convert(int,null),
  13266.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13267.                     case p.action
  13268.                     when 193 then N'SELECT'
  13269.                     when 195 then N'INSERT'
  13270.                     when 197 then N'UPDATE'
  13271.                     else N'REFERENCES'
  13272.                     end),
  13273.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13274.     from 
  13275.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13276.     where
  13277.         o.name = @table_name
  13278.     and     o.type in ('U','V','S')
  13279.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13280.     and    c.id = o.id
  13281.     and     (@column_name is null or @column_name = c.name)
  13282.     and     c.id = p.id
  13283.     and    (@grantor is null or @grantor = user_name(p.grantor))
  13284.     and     case 
  13285.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13286.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13287.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13288.             end & v.high <> 0            /* permission applies to this column */
  13289.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  13290.     and     v.type = 'P'
  13291.     and     v.number = c.colid
  13292.     and    (@grantee is null or @grantee = user_name(u.uid))
  13293.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13294.     and     (u.uid > 0 and u.uid < 16384)
  13295.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  13296.     and     p.protecttype <> 206    /* only grant rows */
  13297.     and     p.action in (26,193,195,197)
  13298.     and     o.uid <> u.uid            /* no rows for owner */
  13299.     and     not exists (            /* exclude revoke'd privileges */
  13300.             select *
  13301.             from sysprotects p1
  13302.             where
  13303.                 p1.protecttype = 206
  13304.             and     p1.action = p.action
  13305.             and     p1.id = p.id
  13306.             and     p1.uid = u.uid
  13307.             and     case 
  13308.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13309.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13310.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13311.                 end & v.high <> 0)            /* permission applies to this column */
  13312.     union
  13313.     select    /*    Add rows for table owner */
  13314.         GRANTOR        = user_name(u.uid),
  13315.         GRANTEE        = user_name(o.uid),
  13316.         TABLE_CATALOG    = db_name(),
  13317.         TABLE_SCHEMA    = user_name(o.uid),
  13318.         TABLE_NAME    = o.name,
  13319.         COLUMN_NAME    = c.name,
  13320.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13321.         COLUMN_PROPID    = convert(int,null),
  13322.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13323.                     case v.number
  13324.                     when 193 then N'SELECT'
  13325.                     when 195 then N'INSERT'
  13326.                     when 197 then N'UPDATE'
  13327.                     else N'REFERENCES'
  13328.                     end),
  13329.         IS_GRANTABLE    = convert(bit,1)    
  13330.     from 
  13331.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13332.     where
  13333.         o.name = @table_name
  13334.     and     o.type in ('U','V','S')
  13335.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13336.     and    (@grantee is null or @grantee = user_name(o.uid))
  13337.     and    c.id = o.id
  13338.     and     (@column_name is null or @column_name = c.name)
  13339.     and     u.uid = 1        /* grantor is dbo of database */
  13340.     and    (@grantor is null or @grantor = user_name(u.uid))
  13341.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13342.     and     v.number in (26,193,195,197)
  13343.     and     not exists (        /* exclude revoke'd privileges */
  13344.             select *
  13345.             from     sysprotects p1
  13346.             where    p1.protecttype = 206
  13347.             and     p1.action = v.number
  13348.             and     p1.id = o.id
  13349.             and     p1.uid = o.uid)
  13350.     order by 4, 5, 6, 9, 1, 2
  13351. go
  13352. dump tran master with no_log
  13353. go
  13354. CREATE PROCEDURE sp_column_privileges_rowset;2
  13355.     (
  13356.     @table_schema        sysname = null,
  13357.     @column_name        sysname = null,
  13358.     @grantor        sysname = null,
  13359.     @grantee        sysname = null
  13360.     )
  13361. as
  13362.     select
  13363.         GRANTOR        = user_name(p.grantor),
  13364.         GRANTEE        = user_name(u.uid),
  13365.         TABLE_CATALOG    = db_name(),
  13366.         TABLE_SCHEMA    = user_name(o.uid),
  13367.         TABLE_NAME    = o.name,
  13368.         COLUMN_NAME    = c.name,
  13369.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13370.         COLUMN_PROPID    = convert(int,null),
  13371.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13372.                     case p.action
  13373.                     when 193 then N'SELECT'
  13374.                     when 195 then N'INSERT'
  13375.                     when 197 then N'UPDATE'
  13376.                     else N'REFERENCES'
  13377.                     end),
  13378.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  13379.     from 
  13380.         sysprotects p, sysobjects o, sysusers u, master.dbo.spt_values v, syscolumns c, sysmembers m
  13381.     where
  13382.         o.type in ('U','V','S')
  13383.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13384.     and    c.id = o.id
  13385.     and     (@column_name is null or @column_name = c.name)
  13386.     and     c.id = p.id
  13387.     and    (@grantor is null or @grantor = user_name(p.grantor))
  13388.     and     case 
  13389.             when substring(p.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13390.             when substring(p.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p.columns, v.low, 1))
  13391.             else (~convert(tinyint, coalesce(substring(p.columns, v.low, 1),0)))
  13392.             end & v.high <> 0            /* permission applies to this column */
  13393.     and     v.number <= (select count(*) from syscolumns where id = o.id)    /* ranges from 1 to # of columns in table */
  13394.     and     v.type = 'P'
  13395.     and     v.number = c.colid
  13396.     and    (@grantee is null or @grantee = user_name(u.uid))
  13397.         /* expand groups - AKUNDONE: only 1 level of group unrolling here. Need more?? */
  13398.     and     (u.uid > 0 and u.uid < 16384)
  13399.     and     ((p.uid = u.uid) or  (p.uid = m.groupuid and u.uid = m.memberuid))    
  13400.     and     p.protecttype <> 206    /* only grant rows */
  13401.     and     p.action in (26,193,195,197)
  13402.     and     o.uid <> u.uid            /* no rows for owner */
  13403.     and     not exists (            /* exclude revoke'd privileges */
  13404.             select *
  13405.             from sysprotects p1
  13406.             where
  13407.                 p1.protecttype = 206
  13408.             and     p1.action = p.action
  13409.             and     p1.id = p.id
  13410.             and     p1.uid = u.uid
  13411.             and     case 
  13412.                 when substring(p1.columns, 1, 1) & 1 is null then 255    /* all columns have permission */
  13413.                 when substring(p1.columns, 1, 1) & 1 = 0 then convert(tinyint, substring(p1.columns, v.low, 1))
  13414.                                 else (~convert(tinyint,coalesce(substring(p.columns, v.low, 1),0)))
  13415.                 end & v.high <> 0)            /* permission applies to this column */
  13416.     union
  13417.     select    /*    Add rows for table owner */
  13418.         GRANTOR        = user_name(u.uid),
  13419.         GRANTEE        = user_name(o.uid),
  13420.         TABLE_CATALOG    = db_name(),
  13421.         TABLE_SCHEMA    = user_name(o.uid),
  13422.         TABLE_NAME    = o.name,
  13423.         COLUMN_NAME    = c.name,
  13424.         COLUMN_GUID    = convert(uniqueidentifier,null),
  13425.         COLUMN_PROPID    = convert(int,null),
  13426.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  13427.                     case v.number
  13428.                     when 193 then N'SELECT'
  13429.                     when 195 then N'INSERT'
  13430.                     when 197 then N'UPDATE'
  13431.                     else N'REFERENCES'
  13432.                     end),
  13433.         IS_GRANTABLE    = convert(bit,1)    
  13434.     from 
  13435.         sysobjects o, master.dbo.spt_values v, sysusers u, syscolumns c
  13436.     where
  13437.         o.type in ('U','V','S')
  13438.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13439.     and    (@grantee is null or @grantee = user_name(o.uid))
  13440.     and    c.id = o.id
  13441.     and     (@column_name is null or @column_name = c.name)
  13442.     and     u.uid = 1        /* grantor is dbo of database */
  13443.     and    (@grantor is null or @grantor = user_name(u.uid))
  13444.     and     v.type = 'P'        /* cross product to get all exposed privileges */
  13445.     and     v.number in (26,193,195,197)
  13446.     and     not exists (        /* exclude revoke'd privileges */
  13447.             select *
  13448.             from     sysprotects p1
  13449.             where    p1.protecttype = 206
  13450.             and     p1.action = v.number
  13451.             and     p1.id = o.id
  13452.             and     p1.uid = o.uid)
  13453.     order by 4, 5, 6, 9, 1, 2
  13454. go
  13455. dump tran master with no_log
  13456. go
  13457. create procedure sp_column_privileges_rowset;5
  13458.     (
  13459.     @table_server        sysname,
  13460.     @table_catalog        sysname = null,
  13461.     @table_name        sysname = null,
  13462.     @table_schema        sysname = null,
  13463.     @column_name        sysname = null,
  13464.     @grantor        sysname = null,
  13465.     @grantee        sysname = null
  13466.     )
  13467. as
  13468.     select
  13469.         GRANTOR,
  13470.         GRANTEE,
  13471.         TABLE_CATALOG,
  13472.         TABLE_SCHEMA,
  13473.         TABLE_NAME,
  13474.         COLUMN_NAME,
  13475.         COLUMN_GUID,
  13476.         COLUMN_PROPID,    
  13477.         PRIVILEGE_TYPE,
  13478.         IS_GRANTABLE    
  13479.         from master.dbo.SYSREMOTE_COLUMN_PRIVILEGES <
  13480.                 @table_server,
  13481.                 @table_catalog,
  13482.                 @table_schema,
  13483.                 @table_name,
  13484.                 @column_name,
  13485.                 @grantor,
  13486.                 @grantee>
  13487.     order by 3, 4, 5, 6, 9, 1, 2
  13488. go
  13489.  
  13490. grant execute on sp_column_privileges_rowset to public
  13491. go
  13492.  
  13493. dump tran master with no_log
  13494. go
  13495. if (charindex('6.00', @@version) > 0)
  13496.     begin
  13497.     if (exists (select * from sysobjects
  13498.             where name = 'sp_column_privileges_rowset' and type = 'P '))
  13499.         begin
  13500.         drop procedure sp_column_privileges_rowset
  13501.         dump tran master with no_log
  13502.         end
  13503.     end
  13504. go
  13505.  
  13506.  
  13507. print ''
  13508. print 'creating sp_columns_rowset'
  13509. go
  13510.  
  13511. /*    Procedure for 6.0 and 6.50 server */
  13512. create procedure sp_columns_rowset
  13513.     (
  13514.        @table_name    varchar(255),
  13515.     @table_schema     varchar(255) = null,
  13516.     @column_name    varchar(255) = null
  13517.     )
  13518. as     
  13519.     select
  13520.         TABLE_CATALOG        = db_name(),
  13521.         TABLE_SCHEMA        = user_name(o.uid),
  13522.         TABLE_NAME        = o.name,
  13523.         COLUMN_NAME        = c.name,
  13524.         COLUMN_GUID        = convert(binary(16),null),
  13525.         COLUMN_PROPID        = convert(int,null),
  13526.         ORDINAL_POSITION     = convert(int,
  13527.                        (
  13528.                         select count(*)
  13529.                         from syscolumns sc
  13530.                         where sc.id     =  c.id
  13531.                           AND sc.number =  c.number
  13532.                           AND sc.colid  <= c.colid
  13533.                         )),
  13534.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13535.  
  13536.         COLUMN_DEFAULT        = convert (varchar (255),
  13537.                                   case when substring(m.text,1,1) = '('
  13538.                                   then substring(m.text,2,datalength(m.text)-2)
  13539.                                   else substring(m.text,1,datalength(m.text)-2)
  13540.                               end), 
  13541.         COLUMN_FLAGS        = convert(int,
  13542.                         case when d.is_long = 1 
  13543.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13544.                         else 0
  13545.                         end
  13546.                     |    case when d.fixlen is not null
  13547.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13548.                         else 0
  13549.                         end
  13550.                     |    case when c.status&8 = 8
  13551.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13552.                         else 0
  13553.                         end
  13554.                     |    case 
  13555.                         when d.type_name = 'timestamp'
  13556.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13557.                         when (c.status&128) != 128
  13558.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13559.                         else 0 
  13560.                         end),
  13561.         IS_NULLABLE        = convert(bit,c.status&8),
  13562.         DATA_TYPE        = d.oledb_data_type,
  13563.         TYPE_GUID        = convert(binary(16),null),
  13564.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13565.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13566.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13567.                         then coalesce(d.column_size,c.length)
  13568.                         else null 
  13569.                         end),
  13570.         CHARACTER_OCTET_LENGTH    = convert(int,
  13571.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13572.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13573.                         then coalesce(d.column_size,c.length)
  13574.                         else null 
  13575.                         end),
  13576.         NUMERIC_PRECISION    = convert(smallint,
  13577.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13578.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13579.                                     then d.data_precision else null end),
  13580.         NUMERIC_SCALE        = convert(smallint, 
  13581.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13582.         DATETIME_PRECISION    = convert(int,
  13583.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13584.                             when data_precision = 23 then 3 else 0 end),
  13585.         CHARACTER_SET_CATALOG    = convert(sysname,
  13586.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13587.                         then 'master' 
  13588.                         else null 
  13589.                         end),
  13590.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13591.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13592.                         then 'dbo' 
  13593.                         else null 
  13594.                         end),
  13595.         CHARACTER_SET_NAME    = convert(sysname,
  13596.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13597.                         then a_cha.name 
  13598.                         else null 
  13599.                         end),
  13600.         COLLATION_CATALOG    = convert(sysname,
  13601.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13602.                         then 'master' 
  13603.                         else null 
  13604.                         end),
  13605.         COLLATION_SCHEMA    = convert(sysname,
  13606.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13607.                         then 'dbo' 
  13608.                         else null 
  13609.                         end),
  13610.         COLLATION_NAME        = convert(sysname,
  13611.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13612.                         then b_cha.name 
  13613.                         else null 
  13614.                         end),
  13615.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13616.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13617.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13618.         DESCRIPTION        = convert(varchar(1),null)    
  13619.     
  13620.     from
  13621.         syscolumns c,
  13622.         syscomments m,
  13623.         sysobjects o,
  13624.         master.dbo.spt_provider_types d,
  13625.         systypes t,
  13626.         sysusers u,
  13627.         master.dbo.sysconfigures    cfg,
  13628.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  13629.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset. */
  13630.     where
  13631.         o.name = @table_name
  13632.     and     o.type in ('U','V','S')
  13633.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13634.     and     (
  13635.         suser_id() = 1     /* User is the System Administrator */
  13636.         or o.uid = user_id()     /* User created the object */
  13637.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13638.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13639.             from sysprotects p
  13640.             where p.id = o.id
  13641.             /* get rows for public,current user,user's group */
  13642.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13643.             /* check for SELECT,EXECUTE privilege */
  13644.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13645.             ) = 1    /* final magic...compare Grants      */
  13646.            )
  13647.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13648.     and    (@column_name is null or @column_name = c.name)
  13649.     and     o.id = c.id
  13650.     and     t.type = d.ss_dtype
  13651.     and    (t.usertype != 80 or d.type_name='timestamp')
  13652.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13653.     and     c.usertype = t.usertype
  13654.     and    c.cdefault *= m.id
  13655.     and    m.colid = 1
  13656.     and    cfg.comment = 'default sortorder ID' 
  13657.     and    a_cha.type = 1001 /* type is charset */
  13658.     and    b_cha.type = 2001 /* type is sortorder */
  13659.     and    a_cha.id = b_cha.csid
  13660.     and     b_cha.id = cfg.value
  13661.     order by 2, 3, c.colid
  13662. go
  13663.  
  13664. dump tran master with no_log
  13665. go
  13666.  
  13667. create procedure sp_columns_rowset;2
  13668.     (
  13669.     @table_schema     varchar(255) = null,
  13670.     @column_name    varchar(255) = null
  13671.     )
  13672. as     
  13673.     select
  13674.         TABLE_CATALOG        = db_name(),
  13675.         TABLE_SCHEMA        = user_name(o.uid),
  13676.         TABLE_NAME        = o.name,
  13677.         COLUMN_NAME        = c.name,
  13678.         COLUMN_GUID        = convert(binary(16),null),
  13679.         COLUMN_PROPID        = convert(int,null),
  13680.         ORDINAL_POSITION     = convert(int,
  13681.                        (
  13682.                         select count(*)
  13683.                         from syscolumns sc
  13684.                         where sc.id     =  c.id
  13685.                           AND sc.number =  c.number
  13686.                           AND sc.colid  <= c.colid
  13687.                         )),
  13688.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13689.         COLUMN_DEFAULT        = convert (varchar (255),
  13690.                                   case when substring(m.text,1,1) = '('
  13691.                                   then substring(m.text,2,datalength(m.text)-2)
  13692.                                   else substring(m.text,1,datalength(m.text)-2)
  13693.                               end), 
  13694.         COLUMN_FLAGS        = convert(int,
  13695.                         case when d.is_long = 1 
  13696.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13697.                         else 0
  13698.                         end
  13699.                     |    case when d.fixlen is not null
  13700.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13701.                         else 0
  13702.                         end
  13703.                     |    case when c.status&8 = 8
  13704.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13705.                         else 0
  13706.                         end
  13707.                     |    case 
  13708.                         when d.type_name = 'timestamp'
  13709.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13710.                         when (c.status&128) != 128
  13711.                             then 0x8 /*DBCOLUMNFLAGS_WRITEUNKNOWN*/ 
  13712.                         else 0 
  13713.                         end),
  13714.         IS_NULLABLE        = convert(bit,c.status&8),
  13715.         DATA_TYPE        = d.oledb_data_type,
  13716.         TYPE_GUID        = convert(binary(16),null),
  13717.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13718.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13719.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13720.                         then coalesce(d.column_size,c.length)
  13721.                         else null 
  13722.                         end),
  13723.         CHARACTER_OCTET_LENGTH    = convert(int,
  13724.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13725.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13726.                         then coalesce(d.column_size,c.length)
  13727.                         else null 
  13728.                         end),
  13729.         NUMERIC_PRECISION    = convert(smallint,
  13730.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  13731.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13732.                             then d.data_precision else null end),
  13733.         NUMERIC_SCALE        = convert(smallint, 
  13734.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13735.         DATETIME_PRECISION    = convert(int,
  13736.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13737.                             when data_precision = 23 then 3 else 0 end),
  13738.         CHARACTER_SET_CATALOG    = convert(sysname,
  13739.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13740.                         then 'master' 
  13741.                         else null 
  13742.                         end),
  13743.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13744.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13745.                         then 'dbo' 
  13746.                         else null 
  13747.                         end),
  13748.         CHARACTER_SET_NAME    = convert(sysname,
  13749.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13750.                         then a_cha.name 
  13751.                         else null 
  13752.                         end),
  13753.         COLLATION_CATALOG    = convert(sysname,
  13754.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13755.                         then 'master' 
  13756.                         else null 
  13757.                         end),
  13758.         COLLATION_SCHEMA    = convert(sysname,
  13759.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13760.                         then 'dbo' 
  13761.                         else null 
  13762.                         end),
  13763.         COLLATION_NAME        = convert(sysname,
  13764.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13765.                         then b_cha.name 
  13766.                         else null 
  13767.                         end),
  13768.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13769.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13770.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13771.         DESCRIPTION        = convert(varchar(1),null)    
  13772.     
  13773.     from
  13774.         syscolumns c,
  13775.         syscomments m,
  13776.         sysobjects o,
  13777.         master.dbo.spt_provider_types d,
  13778.         systypes t,
  13779.         sysusers u,
  13780.         master.dbo.sysconfigures    cfg,
  13781.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  13782.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  13783.     where
  13784.          o.type in ('U','V','S')
  13785.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  13786.     and     (
  13787.         suser_id() = 1     /* User is the System Administrator */
  13788.         or o.uid = user_id()     /* User created the object */
  13789.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  13790.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  13791.             from sysprotects p
  13792.             where p.id = o.id
  13793.             /* get rows for public,current user,user's group */
  13794.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  13795.             /* check for SELECT,EXECUTE privilege */
  13796.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  13797.             ) = 1    /* final magic...compare Grants      */
  13798.            )
  13799.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13800.     and    (@column_name is null or @column_name = c.name)
  13801.     and     o.id = c.id
  13802.     and     t.type = d.ss_dtype
  13803.     and    (t.usertype != 80 or d.type_name='timestamp')
  13804.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13805.     and     c.usertype = t.usertype
  13806.     and    c.cdefault *= m.id
  13807.     and     m.colid = 1
  13808.     and    cfg.comment = 'default sortorder ID' 
  13809.     and    a_cha.type = 1001 /* type is charset */
  13810.     and    b_cha.type = 2001 /* type is sortorder */
  13811.     and    a_cha.id = b_cha.csid
  13812.     and     b_cha.id = cfg.value
  13813.     order by 2, 3, c.colid
  13814.     
  13815. go
  13816.  
  13817. dump tran master with no_log
  13818. go
  13819.  
  13820. if (charindex('7.00', @@version) = 0 and
  13821.     charindex('8.00', @@version) = 0)
  13822. begin
  13823.     print ''
  13824.     print ''
  13825.     print 'Warning:'
  13826.     print 'you are installing the stored procedures '
  13827.     print 'on a pre 8.0 SQL Server.'
  13828.     print 'Ignore the following errors.'
  13829. end
  13830. else
  13831.     drop proc sp_columns_rowset
  13832. go
  13833.  
  13834. /*    Procedure for 7.0 server */
  13835. create procedure sp_columns_rowset
  13836.     (
  13837.        @table_name    sysname,
  13838.     @table_schema     sysname = NULL,
  13839.     @column_name    sysname = NULL
  13840.     )
  13841. as
  13842.     select
  13843.         TABLE_CATALOG        = db_name(),
  13844.         TABLE_SCHEMA        = user_name(o.uid),
  13845.         TABLE_NAME        = o.name,
  13846.         COLUMN_NAME        = c.name,
  13847.         COLUMN_GUID        = convert(uniqueidentifier,null),
  13848.         COLUMN_PROPID        = convert(int,null),
  13849.         ORDINAL_POSITION     = convert(int,
  13850.                        (
  13851.                         select count(*)
  13852.                         from syscolumns sc
  13853.                         where sc.id     =  c.id
  13854.                           AND sc.number =  c.number
  13855.                           AND sc.colid  <= c.colid
  13856.                         )),
  13857.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13858.         COLUMN_DEFAULT        =  convert(nvarchar(2000),
  13859.                                         case when substring(m.text,1,1) = '('
  13860.                                             then substring(m.text,2,datalength(m.text)/2-2)
  13861.                                             else substring(m.text,1,datalength(m.text)/2-2)
  13862.                                         end    ), 
  13863.         COLUMN_FLAGS        = convert(int,
  13864.                         case when d.is_long = 1 
  13865.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  13866.                         else 0
  13867.                         end
  13868.                     |    case when d.fixlen is not null
  13869.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  13870.                         else 0
  13871.                         end
  13872.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  13873.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  13874.                         else 0
  13875.                         end
  13876.                     |    case 
  13877.                         when d.type_name = 'timestamp'
  13878.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  13879.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  13880.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  13881.                         else 0 
  13882.                         end),
  13883.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  13884.         DATA_TYPE        = d.oledb_data_type,
  13885.         TYPE_GUID        = convert(uniqueidentifier,null),
  13886.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  13887.                         case 
  13888.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13889.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13890.                         then coalesce(d.column_size,c.length)
  13891.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13892.                         then coalesce(d.column_size,c.length/2)
  13893.                         else null 
  13894.                         end),
  13895.         CHARACTER_OCTET_LENGTH    = convert(int,
  13896.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  13897.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  13898.                         then coalesce(d.column_size,c.length)
  13899.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  13900.                         then coalesce(d.column_size*2,c.length)
  13901.                         else null 
  13902.                         end),
  13903.         NUMERIC_PRECISION    = convert(smallint,
  13904.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  13905.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  13906.                             then d.data_precision else null end),
  13907.         NUMERIC_SCALE        = convert(smallint, 
  13908.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  13909.         DATETIME_PRECISION    = convert(int,
  13910.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  13911.                             when data_precision = 23 then 3 else 0 end),
  13912.         CHARACTER_SET_CATALOG    = convert(sysname,
  13913.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13914.                         then N'master' 
  13915.                         else null 
  13916.                         end),
  13917.         CHARACTER_SET_SCHEMA    = convert(sysname,
  13918.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13919.                         then N'dbo' 
  13920.                         else null 
  13921.                         end),
  13922.         CHARACTER_SET_NAME    = convert(sysname,
  13923.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13924.                         then a_cha.name 
  13925.                         else null 
  13926.                         end),
  13927.         COLLATION_CATALOG    = convert(sysname,
  13928.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13929.                         then N'master' 
  13930.                         else null 
  13931.                         end),
  13932.         COLLATION_SCHEMA    = convert(sysname,
  13933.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13934.                         then N'dbo' 
  13935.                         else null 
  13936.                         end),
  13937.         COLLATION_NAME        = convert(sysname,
  13938.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  13939.                         then b_cha.name 
  13940.                         else null 
  13941.                         end),
  13942.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  13943.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  13944.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  13945.         DESCRIPTION        = convert(nvarchar(1),null)    
  13946.  
  13947.     from
  13948.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  13949.         sysobjects o,
  13950.         master.dbo.spt_provider_types d,
  13951.         systypes t,
  13952.         master.dbo.sysconfigures    cfg,
  13953.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder. */
  13954.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset. */
  13955.     where
  13956.         permissions(o.id, c.name) <> 0
  13957.     and    o.name = @table_name
  13958.     and     o.type in ('U','V','S')
  13959.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  13960.     and    (@column_name is null or @column_name = c.name)
  13961.     and     o.id = c.id
  13962.     and     t.xtype = d.ss_dtype
  13963.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  13964.     and     c.xusertype = t.xusertype
  13965.     and    cfg.comment = 'default sortorder id' 
  13966.     and    a_cha.type = 1001 /* type is charset */
  13967.     and    b_cha.type = 2001 /* type is sortorder */
  13968.     and    a_cha.id = b_cha.csid
  13969.     and     b_cha.id = cfg.value
  13970.     order by 2, 3, c.colorder
  13971. go
  13972.  
  13973. dump tran master with no_log
  13974. go
  13975.  
  13976. /*    Procedure for 7.0 server */
  13977. create procedure sp_columns_rowset;2
  13978.     (
  13979.     @table_schema     sysname = NULL,
  13980.     @column_name    sysname = NULL
  13981.     )
  13982. as     
  13983.     select
  13984.         TABLE_CATALOG        = db_name(),
  13985.         TABLE_SCHEMA        = user_name(o.uid),
  13986.         TABLE_NAME        = o.name,
  13987.         COLUMN_NAME        = c.name,
  13988.         COLUMN_GUID        = convert(uniqueidentifier,null),
  13989.         COLUMN_PROPID        = convert(int,null),
  13990.         ORDINAL_POSITION     = convert(int,
  13991.                        (
  13992.                         select count(*)
  13993.                         from syscolumns sc
  13994.                         where sc.id     =  c.id
  13995.                           AND sc.number =  c.number
  13996.                           AND sc.colid  <= c.colid
  13997.                         )),
  13998.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  13999.         COLUMN_DEFAULT        =  convert(nvarchar(2000),
  14000.                                         case when substring(m.text,1,1) = '('
  14001.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14002.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14003.                                         end    ), 
  14004.         COLUMN_FLAGS        = convert(int,
  14005.                         case when d.is_long = 1 
  14006.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14007.                         else 0
  14008.                         end
  14009.                     |    case when d.fixlen is not null
  14010.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14011.                         else 0
  14012.                         end
  14013.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14014.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14015.                         else 0
  14016.                         end
  14017.                     |    case 
  14018.                         when d.type_name = 'timestamp'
  14019.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14020.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  14021.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14022.                         else 0 
  14023.                         end),
  14024.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14025.         DATA_TYPE        = d.oledb_data_type,
  14026.         TYPE_GUID        = convert(uniqueidentifier,null),
  14027.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14028.                         case 
  14029.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14030.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14031.                         then coalesce(d.column_size,c.length)
  14032.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14033.                         then coalesce(d.column_size,c.length/2)
  14034.                         else null 
  14035.                         end),
  14036.         CHARACTER_OCTET_LENGTH    = convert(int,
  14037.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14038.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14039.                         then coalesce(d.column_size,c.length)
  14040.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14041.                         then coalesce(d.column_size*2,c.length)
  14042.                         else null 
  14043.                         end),
  14044.         NUMERIC_PRECISION    = convert(smallint,
  14045.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14046.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14047.                             then d.data_precision else null end),
  14048.         NUMERIC_SCALE        = convert(smallint, 
  14049.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14050.         DATETIME_PRECISION    = convert(int,
  14051.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14052.                             when data_precision = 23 then 3 else 0 end),
  14053.         CHARACTER_SET_CATALOG    = convert(sysname,
  14054.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14055.                         then N'master' 
  14056.                         else null 
  14057.                         end),
  14058.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14059.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14060.                         then N'dbo' 
  14061.                         else null 
  14062.                         end),
  14063.         CHARACTER_SET_NAME    = convert(sysname,
  14064.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14065.                         then a_cha.name 
  14066.                         else null 
  14067.                         end),
  14068.         COLLATION_CATALOG    = convert(sysname,
  14069.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14070.                         then N'master' 
  14071.                         else null 
  14072.                         end),
  14073.         COLLATION_SCHEMA    = convert(sysname,
  14074.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14075.                         then N'dbo' 
  14076.                         else null 
  14077.                         end),
  14078.         COLLATION_NAME        = convert(sysname,
  14079.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14080.                         then b_cha.name 
  14081.                         else null 
  14082.                         end),
  14083.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  14084.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  14085.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  14086.         DESCRIPTION        = convert(nvarchar(1),null)    
  14087.     
  14088.     from
  14089.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14090.         sysobjects o,
  14091.         master.dbo.spt_provider_types d,
  14092.         systypes t,
  14093.         master.dbo.sysconfigures    cfg,
  14094.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder.*/
  14095.         master.dbo.syscharsets        b_cha /* sortorder/2001, not charset.*/
  14096.     where
  14097.         permissions(o.id, c.name) <> 0
  14098.     and     o.type in ('U','V','S')
  14099.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  14100.     and    (@column_name is null or @column_name = c.name)
  14101.     and     o.id = c.id
  14102.     and     t.xtype = d.ss_dtype
  14103.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14104.     and     c.xusertype = t.xusertype
  14105.     and    cfg.comment = 'default sortorder id' 
  14106.     and     a_cha.type = 1001 /* type is charset */
  14107.     and     b_cha.type = 2001 /* type is sortorder */
  14108.     and    a_cha.id = b_cha.csid
  14109.     and     b_cha.id = cfg.value
  14110.     order by 2, 3, c.colorder
  14111. go
  14112.  
  14113. dump tran master with no_log
  14114. go
  14115.  
  14116. /*    Procedure for 7.0 server */
  14117. create procedure sp_columns_rowset;5
  14118.     (
  14119.     @table_server        sysname,
  14120.     @table_catalog        sysname = null,
  14121.     @table_name        sysname = null,
  14122.     @table_schema        sysname = null,
  14123.     @column_name        sysname = null
  14124.     )
  14125. as
  14126.     select
  14127.         TABLE_CATALOG,
  14128.         TABLE_SCHEMA,
  14129.         TABLE_NAME,
  14130.         COLUMN_NAME,
  14131.         COLUMN_GUID,
  14132.         COLUMN_PROPID,
  14133.         ORDINAL_POSITION,
  14134.         COLUMN_HASDEFAULT,
  14135.         COLUMN_DEFAULT,
  14136.         COLUMN_FLAGS,
  14137.         IS_NULLABLE,
  14138.         DATA_TYPE,
  14139.         TYPE_GUID,
  14140.         CHARACTER_MAXIMUM_LENGTH,
  14141.         CHARACTER_OCTET_LENGTH,
  14142.         NUMERIC_PRECISION,
  14143.         NUMERIC_SCALE,
  14144.         DATETIME_PRECISION,
  14145.         CHARACTER_SET_CATALOG,
  14146.         CHARACTER_SET_SCHEMA,
  14147.         CHARACTER_SET_NAME,
  14148.         COLLATION_CATALOG,
  14149.         COLLATION_SCHEMA,
  14150.         COLLATION_NAME,
  14151.         DOMAIN_CATALOG,
  14152.         DOMAIN_SCHEMA,
  14153.         DOMAIN_NAME,
  14154.         DESCRIPTION
  14155.     from master.dbo.SYSREMOTE_COLUMNS <
  14156.                 @table_server,
  14157.                 @table_catalog,
  14158.                 @table_schema,
  14159.                 @table_name,
  14160.                 @column_name > 
  14161.     order by 1, 2, 3, 7
  14162. go
  14163.  
  14164.  
  14165. dump tran master with no_log
  14166. go
  14167.  
  14168. /*    Procedure for 8.00 server */
  14169. if (charindex('8.00', @@version) = 0)
  14170. begin
  14171.     print ''
  14172.     print ''
  14173.     print 'Warning:'
  14174.     print 'you are installing the stored procedures '
  14175.     print 'on a pre 8.0 SQL Server.'
  14176.     print 'Ignore the following errors.'
  14177. end
  14178. else
  14179.     drop proc sp_columns_rowset
  14180. go
  14181.  
  14182. /*    Procedure for 8.00 server */
  14183. create procedure sp_columns_rowset
  14184.     (
  14185.        @table_name    sysname,
  14186.     @table_schema     sysname = NULL,
  14187.     @column_name    sysname = NULL
  14188.     )
  14189. as     
  14190.     select
  14191.         TABLE_CATALOG        = db_name(),
  14192.         TABLE_SCHEMA        = user_name(o.uid),
  14193.         TABLE_NAME        = o.name,
  14194.         COLUMN_NAME        = c.name,
  14195.         COLUMN_GUID        = convert(uniqueidentifier,null),
  14196.         COLUMN_PROPID        = convert(int,null),
  14197.         ORDINAL_POSITION     = convert(int,
  14198.                        (
  14199.                         select count(*)
  14200.                         from syscolumns sc
  14201.                         where sc.id     =  c.id
  14202.                           AND sc.number =  c.number
  14203.                           AND sc.colid  <= c.colid
  14204.                         )),
  14205.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  14206.  
  14207.         /* Get rid of ( if it is first char otherwise display the whole thing */
  14208.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  14209.                                         case when substring(m.text,1,1) = '('
  14210.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14211.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14212.                                         end    ), 
  14213.  
  14214.         COLUMN_FLAGS        = convert(int,
  14215.                         case when d.is_long = 1 
  14216.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14217.                         else 0
  14218.                         end
  14219.                     |    case when d.fixlen is not null
  14220.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14221.                         else 0
  14222.                         end
  14223.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14224.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14225.                         else 0
  14226.                         end
  14227.                     |    case 
  14228.                         when d.type_name = 'timestamp'
  14229.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14230.                         when (c.status&128) != 128  and permissions(o.id,c.name)&2 = 2
  14231.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14232.                         else 0 
  14233.                         end),
  14234.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14235.         DATA_TYPE        = d.oledb_data_type,
  14236.         TYPE_GUID        = convert(uniqueidentifier,null),
  14237.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14238.                         case 
  14239.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14240.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14241.                         then coalesce(d.column_size,c.length)
  14242.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14243.                         then coalesce(d.column_size,c.length/2)
  14244.                         else null 
  14245.                         end),
  14246.         CHARACTER_OCTET_LENGTH    = convert(int,
  14247.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14248.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14249.                         then coalesce(d.column_size,c.length)
  14250.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14251.                         then coalesce(d.column_size*2,c.length)
  14252.                         else null 
  14253.                         end),
  14254.         NUMERIC_PRECISION    = convert(smallint,
  14255.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14256.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14257.                             then d.data_precision else null end),
  14258.         NUMERIC_SCALE        = convert(smallint, 
  14259.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14260.         DATETIME_PRECISION    = convert(int,
  14261.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14262.                             when data_precision = 23 then 3 else 0 end),
  14263.         CHARACTER_SET_CATALOG    = convert(sysname,
  14264.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14265.                         then N'master' 
  14266.                         else null 
  14267.                         end),
  14268.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14269.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14270.                         then N'dbo' 
  14271.                         else null 
  14272.                         end),
  14273.         CHARACTER_SET_NAME    = convert(sysname,
  14274.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14275.                         then a_cha.name 
  14276.                         else null 
  14277.                         end),
  14278.         COLLATION_CATALOG    = convert(sysname,
  14279.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14280.                         then N'master' 
  14281.                         else null 
  14282.                         end),
  14283.         COLLATION_SCHEMA    = convert(sysname,
  14284.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14285.                         then N'dbo' 
  14286.                         else null 
  14287.                         end),
  14288.         COLLATION_NAME        = convert(sysname,
  14289.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14290.                         then c.collation 
  14291.                         else null 
  14292.                         end),
  14293.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  14294.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  14295.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  14296.         DESCRIPTION        = convert(nvarchar(1),null),
  14297.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14298.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14299.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14300.                                when ABS(c.collationid) > 0x1000000 
  14301.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14302.                             else null 
  14303.                         end,
  14304.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14305.         IS_COMPUTED    = convert(bit, c.iscomputed)    
  14306.     from
  14307.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14308.         sysobjects o,
  14309.         master.dbo.spt_provider_types d,
  14310.         systypes t,
  14311.         master.dbo.syscharsets        a_cha /* charset/1001, not sortorder. */
  14312.     where
  14313.         permissions(o.id, c.name) <> 0
  14314.     and    o.name = @table_name
  14315.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14316.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  14317.     and    (@column_name is null or @column_name = c.name)
  14318.     and     o.id = c.id
  14319.     and     t.xtype = d.ss_dtype
  14320.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14321.     and     c.xusertype = t.xusertype
  14322.     and    a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14323.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14324.     order by 2, 3, c.colorder
  14325. go
  14326.  
  14327.  
  14328. /*    Procedure for 8.0 server */
  14329. create procedure sp_columns_rowset;2
  14330.     (
  14331.     @table_schema     sysname = NULL,
  14332.     @column_name    sysname = NULL
  14333.     )
  14334. as     
  14335.     select
  14336.         TABLE_CATALOG        = db_name(),
  14337.         TABLE_SCHEMA        = user_name(o.uid),
  14338.         TABLE_NAME        = o.name,
  14339.         COLUMN_NAME        = c.name,
  14340.         COLUMN_GUID        = convert(uniqueidentifier,null),
  14341.         COLUMN_PROPID        = convert(int,null),
  14342.         ORDINAL_POSITION     = convert(int,
  14343.                        (
  14344.                         select count(*)
  14345.                         from syscolumns sc
  14346.                         where sc.id     =  c.id
  14347.                           AND sc.number =  c.number
  14348.                           AND sc.colid  <= c.colid
  14349.                         )),
  14350.         COLUMN_HASDEFAULT    = convert(bit, case when m.text is null then 0 else 1 end),
  14351.         COLUMN_DEFAULT        = convert(nvarchar(2000),
  14352.                                         case when substring(m.text,1,1) = '('
  14353.                                             then substring(m.text,2,datalength(m.text)/2-2)
  14354.                                             else substring(m.text,1,datalength(m.text)/2-2)
  14355.                                         end    ), 
  14356.         COLUMN_FLAGS        = convert(int,
  14357.                         case when d.is_long = 1 
  14358.                         then 0x82 /*DBCOLUMNFLAGS_ISLONG|DBCOLUMNFLAGS_MAYDEFER*/
  14359.                         else 0
  14360.                         end
  14361.                     |    case when d.fixlen is not null
  14362.                         then 0x10 /*DBCOLUMNFLAGS_ISFIXEDLENGTH*/
  14363.                         else 0
  14364.                         end
  14365.                     |    case when ColumnProperty(c.id,c.name,'AllowsNull') = 1
  14366.                         then 0x60 /*DBCOLUMNFLAGS_ISNULLABLE|DBCOLUMNFLAGS_MAYBENULL*/
  14367.                         else 0
  14368.                         end
  14369.                     |    case 
  14370.                         when d.type_name = 'timestamp'
  14371.                             then 0x200 /*DBCOLUMNFLAGS_ISROWVER*/
  14372.                         when (c.status&128) != 128 and permissions(o.id,c.name)&2 = 2
  14373.                             then 0x4 /*DBCOLUMNFLAGS_WRITE*/ 
  14374.                         else 0 
  14375.                         end),
  14376.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  14377.         DATA_TYPE        = d.oledb_data_type,
  14378.         TYPE_GUID        = convert(uniqueidentifier,null),
  14379.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  14380.                         case 
  14381.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14382.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14383.                         then coalesce(d.column_size,c.length)
  14384.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14385.                         then coalesce(d.column_size,c.length/2)
  14386.                         else null 
  14387.                         end),
  14388.         CHARACTER_OCTET_LENGTH    = convert(int,
  14389.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  14390.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  14391.                         then coalesce(d.column_size,c.length)
  14392.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  14393.                         then coalesce(d.column_size*2,c.length)
  14394.                         else null 
  14395.                         end),
  14396.         NUMERIC_PRECISION    = convert(smallint,
  14397.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.xprec
  14398.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  14399.                             then d.data_precision else null end),
  14400.         NUMERIC_SCALE        = convert(smallint, 
  14401.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  14402.         DATETIME_PRECISION    = convert(int,
  14403.                         case when d.oledb_data_type <> 135 /*DBTYPE_DBTIMESTAMP*/ then null
  14404.                             when data_precision = 23 then 3 else 0 end),
  14405.         CHARACTER_SET_CATALOG    = convert(sysname,
  14406.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14407.                         then N'master' 
  14408.                         else null 
  14409.                         end),
  14410.         CHARACTER_SET_SCHEMA    = convert(sysname,
  14411.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14412.                         then N'dbo' 
  14413.                         else null 
  14414.                         end),
  14415.         CHARACTER_SET_NAME    = convert(sysname,
  14416.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14417.                         then a_cha.name 
  14418.                         else null 
  14419.                         end),
  14420.         COLLATION_CATALOG    = convert(sysname,
  14421.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14422.                         then N'master' 
  14423.                         else null 
  14424.                         end),
  14425.         COLLATION_SCHEMA    = convert(sysname,
  14426.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14427.                         then N'dbo' 
  14428.                         else null 
  14429.                         end),
  14430.         COLLATION_NAME        = convert(sysname,
  14431.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/
  14432.                         then c.collation 
  14433.                         else null 
  14434.                         end),
  14435.         DOMAIN_CATALOG        = case when t.usertype < 256 then null else db_name() end,
  14436.         DOMAIN_SCHEMA        = case when t.usertype < 256 then null else user_name(o.uid) end,
  14437.         DOMAIN_NAME        = case when t.usertype < 256 then null else t.name end,
  14438.         DESCRIPTION        = convert(nvarchar(1),null),    
  14439.         COLUMN_LCID     = convert(int, CollationPropertyFromID(c.collationid, 'lcid')),
  14440.         COLUMN_COMPFLAGS = convert(int, CollationPropertyFromID(c.collationid, 'oledbcompstyle')),
  14441.         COLUMN_SORTID = case /* hack to keep the old behavior: will be removed */
  14442.                                when ABS(c.collationid) > 0x1000000 
  14443.                             then convert(int, DATABASEPROPERTYEX(db_name(),'sqlsortorder'))
  14444.                             else null 
  14445.                         end,
  14446.         
  14447.         COLUMN_TDSCOLLATION = convert (binary(5),CollationPropertyFromID(c.collationid, 'TDSCollation')),
  14448.         IS_COMPUTED = convert(bit, c.iscomputed)
  14449.     
  14450.     from
  14451.         syscolumns c left join syscomments m on c.cdefault = m.id and m.colid = 1,
  14452.         sysobjects o,
  14453.         master.dbo.spt_provider_types d,
  14454.         systypes t,
  14455.         master.dbo.syscharsets        a_cha /* charset/1001, not sortorder.*/
  14456.     where
  14457.         permissions(o.id, c.name) <> 0
  14458.     and     (o.type in ('U','V','S') OR (o.type in ('TF', 'IF') and c.number = 0))
  14459.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  14460.     and    (@column_name is null or @column_name = c.name)
  14461.     and     o.id = c.id
  14462.     and     t.xtype = d.ss_dtype
  14463.     and     c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  14464.     and     c.xusertype = t.xusertype
  14465.     and    a_cha.type = 1001 /* type is charset */
  14466.     and    a_cha.id = isnull(convert(tinyint, CollationPropertyFromID(c.collationid, 'sqlcharset')),
  14467.             convert(tinyint, ServerProperty('sqlcharset'))) -- make sure there's one and only one row selected for each column
  14468.     order by 2, 3, c.colorder
  14469. go
  14470.  
  14471. dump tran master with no_log
  14472. go
  14473.  
  14474. /*    Procedure for 8.0 server */
  14475. create procedure sp_columns_rowset;5
  14476.     (
  14477.     @table_server        sysname,
  14478.     @table_catalog        sysname = null,
  14479.     @table_name        sysname = null,
  14480.     @table_schema        sysname = null,
  14481.     @column_name        sysname = null
  14482.     )
  14483. as
  14484.     select
  14485.         TABLE_CATALOG,
  14486.         TABLE_SCHEMA,
  14487.         TABLE_NAME,
  14488.         COLUMN_NAME,
  14489.         COLUMN_GUID,
  14490.         COLUMN_PROPID,
  14491.         ORDINAL_POSITION,
  14492.         COLUMN_HASDEFAULT,
  14493.         COLUMN_DEFAULT,
  14494.         COLUMN_FLAGS,
  14495.         IS_NULLABLE,
  14496.         DATA_TYPE,
  14497.         TYPE_GUID,
  14498.         CHARACTER_MAXIMUM_LENGTH,
  14499.         CHARACTER_OCTET_LENGTH,
  14500.         NUMERIC_PRECISION,
  14501.         NUMERIC_SCALE,
  14502.         DATETIME_PRECISION,
  14503.         CHARACTER_SET_CATALOG,
  14504.         CHARACTER_SET_SCHEMA,
  14505.         CHARACTER_SET_NAME,
  14506.         COLLATION_CATALOG,
  14507.         COLLATION_SCHEMA,
  14508.         COLLATION_NAME,
  14509.         DOMAIN_CATALOG,
  14510.         DOMAIN_SCHEMA,
  14511.         DOMAIN_NAME,
  14512.         DESCRIPTION
  14513.  
  14514.     from master.dbo.SYSREMOTE_COLUMNS <
  14515.                 @table_server,
  14516.                 @table_catalog,
  14517.                 @table_schema,
  14518.                 @table_name,
  14519.                 @column_name > 
  14520.     order by 1, 2, 3, 7
  14521. go
  14522.  
  14523.  
  14524. grant execute on sp_columns_rowset to public
  14525. go
  14526.  
  14527. dump tran master with no_log
  14528. go
  14529. if (charindex('6.00', @@version) > 0)
  14530.     begin
  14531.     if (exists (select * from sysobjects
  14532.             where name = 'sp_columns_rowset' and type = 'P '))
  14533.         begin
  14534.         drop procedure sp_columns_rowset
  14535.         dump tran master with no_log
  14536.         end
  14537.     end
  14538. go
  14539.  
  14540. print ''
  14541. print 'creating sp_check_constraints_rowset'
  14542. go
  14543.  
  14544. /*    Procedure for 6.50 and earlier servers */
  14545. create procedure sp_check_constraints_rowset
  14546.     (
  14547.     @constraint_name    varchar(255),
  14548.     @constraint_schema    varchar(255) = null
  14549.     )
  14550. as
  14551.     select
  14552.         CONSTRAINT_CATALOG    = db_name(),
  14553.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14554.         CONSTRAINT_NAME        = c_obj.name,
  14555.         CHECK_CLAUSE        = m.text,    
  14556.         DESCRIPTION            = convert(varchar(1),null)
  14557.     from 
  14558.         sysobjects c_obj, syscomments m
  14559.     where
  14560.             c_obj.type = 'C '
  14561.         and c_obj.name    = @constraint_name
  14562.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14563.         and c_obj.id = m.id
  14564.     order by 1,2,3 
  14565. go
  14566. dump tran master with no_log
  14567. go
  14568.  
  14569. create procedure sp_check_constraints_rowset;2
  14570.     (
  14571.     @constraint_schema    varchar(255) = null
  14572.     )
  14573. as
  14574.     select
  14575.         CONSTRAINT_CATALOG    = db_name(),
  14576.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14577.         CONSTRAINT_NAME        = c_obj.name,
  14578.         CHECK_CLAUSE        = m.text,    
  14579.         DESCRIPTION            = convert(varchar(1),null)
  14580.     from 
  14581.         sysobjects c_obj, syscomments m
  14582.     where
  14583.             c_obj.type = 'C '
  14584.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14585.         and c_obj.id = m.id
  14586.     order by 1,2,3 
  14587. go
  14588. dump tran master with no_log
  14589. go
  14590.  
  14591. if (charindex('7.00', @@version) > 0 or
  14592.     charindex('8.00', @@version) > 0)
  14593.     drop procedure sp_check_constraints_rowset
  14594. else
  14595. begin
  14596.     print ''
  14597.     print ''
  14598.     print 'Warning:'
  14599.     print 'you are installing the stored procedures '
  14600.     print 'on a pre 7.0 SQL Server.'
  14601.     print 'Ignore the following errors.'
  14602. end
  14603. go
  14604.  
  14605. /*    Procedure for 7.0 servers and 8.0 servers */
  14606. create procedure sp_check_constraints_rowset
  14607.     (
  14608.     @constraint_name    sysname,
  14609.     @constraint_schema    sysname = null
  14610.     )
  14611. as
  14612.     select
  14613.         CONSTRAINT_CATALOG    = db_name(),
  14614.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14615.         CONSTRAINT_NAME        = c_obj.name,
  14616.         CHECK_CLAUSE        = m.text,    
  14617.         DESCRIPTION            = convert(nvarchar(1), null)
  14618.     from 
  14619.         sysobjects c_obj, syscomments m
  14620.     where
  14621.             c_obj.type = 'C '
  14622.         and c_obj.name    = @constraint_name
  14623.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14624.         and m.id = c_obj.id
  14625.     order by 1,2,3 
  14626. go
  14627. dump tran master with no_log
  14628. go
  14629.  
  14630. create procedure sp_check_constraints_rowset;2
  14631.     (
  14632.     @constraint_schema    sysname = null
  14633.     )
  14634. as
  14635.     select
  14636.         CONSTRAINT_CATALOG    = db_name(),
  14637.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14638.         CONSTRAINT_NAME        = c_obj.name,
  14639.         CHECK_CLAUSE        = m.text,    
  14640.         DESCRIPTION            = convert(nvarchar(1), null)
  14641.     from 
  14642.         sysobjects c_obj, syscomments m
  14643.     where
  14644.             c_obj.type = 'C '
  14645.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14646.         and m.id = c_obj.id
  14647.     order by 1,2,3 
  14648. go
  14649.  
  14650. grant execute on sp_check_constraints_rowset to public
  14651. go
  14652.  
  14653. dump tran master with no_log
  14654. go
  14655.  
  14656. if (charindex('6.00', @@version) > 0)
  14657.     begin
  14658.     if (exists (select * from sysobjects
  14659.             where name = 'sp_check_constraints_rowset' and type = 'P '))
  14660.         begin
  14661.         drop procedure sp_check_constraints_rowset
  14662.         dump tran master with no_log
  14663.         end
  14664.     end
  14665. go
  14666.  
  14667. print ''
  14668. print 'creating sp_check_constbytable_rowset'
  14669. go
  14670.  
  14671. /*    Procedure for 6.50 and earlier servers */
  14672. create procedure sp_check_constbytable_rowset
  14673.     (
  14674.     @table_name            varchar(255), 
  14675.     @table_schema        varchar(255) = null,
  14676.     @constraint_name    varchar(255) = null,
  14677.     @constraint_schema    varchar(255) = null
  14678.     )
  14679. as
  14680.     select
  14681.         TABLE_CATALOG        = db_name(),
  14682.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14683.         TABLE_NAME            = t_obj.name,
  14684.         CONSTRAINT_CATALOG    = db_name(),
  14685.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14686.         CONSTRAINT_NAME        = c_obj.name,
  14687.         CHECK_CLAUSE        = m.text,    
  14688.         DESCRIPTION            = convert(varchar(1),null)
  14689.     from 
  14690.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14691.     where
  14692.             t_obj.name    = @table_name
  14693.         and t_obj.type in ('U ','S ')
  14694.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))    
  14695.         and t_obj.id = c.id
  14696.         and c.constid = c_obj.id
  14697.         and c_obj.type = 'C '
  14698.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14699.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14700.         and c_obj.id = m.id
  14701.     order by 1,2,3,4,5,6 
  14702. go
  14703. dump tran master with no_log
  14704. go
  14705.  
  14706. create procedure sp_check_constbytable_rowset;2
  14707.     (
  14708.     @table_schema        varchar(255) = null,
  14709.     @constraint_name    varchar(255) = null,
  14710.     @constraint_schema    varchar(255) = null
  14711.     )
  14712. as
  14713.     select
  14714.         TABLE_CATALOG        = db_name(),
  14715.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14716.         TABLE_NAME            = t_obj.name,
  14717.         CONSTRAINT_CATALOG    = db_name(),
  14718.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14719.         CONSTRAINT_NAME        = c_obj.name,
  14720.         CHECK_CLAUSE        = m.text,    
  14721.         DESCRIPTION            = convert(varchar(1),null)
  14722.     from 
  14723.         sysobjects c_obj, sysobjects t_obj, sysconstraints c, syscomments m
  14724.     where
  14725.             t_obj.type in ('U ','S ')
  14726.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14727.         and c.id = t_obj.id
  14728.         and c.constid = c_obj.id
  14729.         and c_obj.type    = 'C '
  14730.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14731.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14732.         and c_obj.id = m.id
  14733.     order by 1,2,3,4,5,6 
  14734. go
  14735. dump tran master with no_log
  14736. go
  14737.  
  14738. if (charindex('7.00', @@version) > 0 or
  14739.     charindex('8.00', @@version) > 0)
  14740.     drop procedure sp_check_constbytable_rowset
  14741. else
  14742. begin
  14743.     print ''
  14744.     print ''
  14745.     print 'Warning:'
  14746.     print 'you are installing the stored procedures '
  14747.     print 'on a pre 8.0 SQL Server.'
  14748.     print 'Ignore the following errors.'
  14749. end
  14750. go
  14751.  
  14752. /*    Procedure for 8.0 server */
  14753. create procedure sp_check_constbytable_rowset
  14754.     (
  14755.     @table_name            sysname, 
  14756.     @table_schema        sysname = null,
  14757.     @constraint_name    sysname = null,
  14758.     @constraint_schema    sysname = null
  14759.     )
  14760. as
  14761.     select
  14762.         TABLE_CATALOG        = db_name(),
  14763.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14764.         TABLE_NAME            = t_obj.name,
  14765.         CONSTRAINT_CATALOG    = db_name(),
  14766.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14767.         CONSTRAINT_NAME        = c_obj.name,
  14768.         CHECK_CLAUSE        = m.text,    
  14769.         DESCRIPTION            = convert(nvarchar(1), null)
  14770.  
  14771.     from 
  14772.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14773.     where
  14774.             t_obj.name    = @table_name
  14775.         and t_obj.type in ('U ','S ')
  14776.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14777.         and c_obj.parent_obj = t_obj.id
  14778.         and c_obj.type = 'C '
  14779.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14780.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14781.         and m.id = c_obj.id
  14782. order by 1,2,3,4,5,6 
  14783. go
  14784. dump tran master with no_log
  14785. go
  14786. create procedure sp_check_constbytable_rowset;2
  14787.     (
  14788.     @table_schema        sysname = null,
  14789.     @constraint_name    sysname = null,
  14790.     @constraint_schema    sysname = null
  14791.     )
  14792. as
  14793.     select
  14794.         TABLE_CATALOG        = db_name(),
  14795.         TABLE_SCHEMA        = user_name(t_obj.uid),
  14796.         TABLE_NAME            = t_obj.name,
  14797.         CONSTRAINT_CATALOG    = db_name(),
  14798.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  14799.         CONSTRAINT_NAME        = c_obj.name,
  14800.         CHECK_CLAUSE        = m.text,    
  14801.         DESCRIPTION            = convert(nvarchar(1), null)
  14802.     from 
  14803.         sysobjects c_obj, sysobjects t_obj, syscomments m
  14804.     where
  14805.             t_obj.type in ('U ','S ')
  14806.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  14807.         and c_obj.parent_obj = t_obj.id
  14808.         and c_obj.type = 'C '
  14809.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  14810.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  14811.         and m.id = c_obj.id
  14812.     order by 1,2,3,4,5,6 
  14813. go
  14814. dump tran master with no_log
  14815. go
  14816.  
  14817. grant execute on sp_check_constbytable_rowset to public
  14818. go
  14819.  
  14820. dump tran master with no_log
  14821. go
  14822. if (charindex('6.00', @@version) > 0)
  14823.     begin
  14824.     if (exists (select * from sysobjects
  14825.             where name = 'sp_check_constbytable_rowset' and type = 'P '))
  14826.         begin
  14827.         drop procedure sp_check_constbytable_rowset
  14828.         dump tran master with no_log
  14829.         end
  14830.     end
  14831. go
  14832.  
  14833.  
  14834. print ''
  14835. print 'creating sp_foreign_keys_rowset'
  14836. go
  14837.  
  14838. /*    Procedure for 6.0 and 6.5 server */
  14839. CREATE PROCEDURE sp_foreign_keys_rowset
  14840.     (
  14841.      @pk_table_name        varchar(255) = null,
  14842.     @pk_table_schema    varchar(255) = null,
  14843.     @pk_table_catalog    varchar(255) = null,
  14844.     @fk_table_name        varchar(255) = null,
  14845.     @fk_table_schema    varchar(255) = null,
  14846.     @fk_table_catalog    varchar(255) = null
  14847.     )
  14848. as
  14849.     BEGIN
  14850.     select
  14851.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  14852.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  14853.         PK_TABLE_NAME         = o1.name,
  14854.         PK_COLUMN_NAME         = c1.name,
  14855.         PK_COLUMN_GUID        = convert(binary(16),null),
  14856.         PK_COLUMN_PROPID    = convert(int,null),
  14857.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  14858.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  14859.         FK_TABLE_NAME         = o2.name,
  14860.         FK_COLUMN_NAME         = c2.name,
  14861.         FK_COLUMN_GUID        = convert(binary(16),null),
  14862.         FK_COLUMN_PROPID    = convert(int,null),
  14863.         ORDINAL                = convert(int,1),
  14864.         UPDATE_RULE            = 'NO ACTION',
  14865.         DELETE_RULE         = 'NO ACTION',
  14866.         PK_NAME                = i.name,
  14867.         FK_NAME                = object_name(r.constid),
  14868.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14869.     from
  14870.         sysobjects o1, sysobjects o2,
  14871.         syscolumns c1, syscolumns c2,
  14872.         sysreferences r, sysindexes i, sysusers u
  14873.     where    
  14874.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14875.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14876.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14877.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14878.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14879.     and    o1.id = r.rkeyid
  14880.     and    o1.id = c1.id
  14881.     and    c1.colid = r.rkey1
  14882.     and r.fkeyid = o2.id
  14883.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14884.     and    o2.id = c2.id    
  14885.     and    c2.colid = r.fkey1
  14886.     and i.id = r.rkeyid
  14887.     and i.indid = r.rkeyindid
  14888.     and u.uid = user_id()
  14889.     and ( suser_id() = 1     /* User is the System Administrator */
  14890.     or (
  14891.         o1.uid = user_id()     /* User created the object */
  14892.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14893.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14894.             from sysprotects p
  14895.             where p.id = o1.id
  14896.             /* get rows for public,current user,user's group */
  14897.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14898.             /* check for SELECT,EXECUTE privilege */
  14899.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14900.             ) = 1    /* final magic...compare Grants      */
  14901.         )
  14902.     and (
  14903.         o2.uid = user_id()     /* User created the object */
  14904.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14905.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14906.             from sysprotects p
  14907.             where p.id = o2.id
  14908.             /* get rows for public,current user,user's group */
  14909.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14910.             /* check for SELECT,EXECUTE privilege */
  14911.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14912.             ) = 1    /* final magic...compare Grants      */
  14913.         )
  14914.     )
  14915.     union all
  14916.     select
  14917.         db_name(r.rkeydbid),
  14918.         user_name(o1.uid),
  14919.         o1.name,
  14920.         c1.name,
  14921.         convert(binary(16),null),
  14922.         convert(int,null),
  14923.         db_name(r.fkeydbid),
  14924.         user_name(o2.uid),
  14925.         o2.name,
  14926.         c2.name,
  14927.         convert(binary(16),null),
  14928.         convert(int,null),
  14929.         convert(int,2),
  14930.         'NO ACTION',
  14931.         'NO ACTION',
  14932.         i.name,
  14933.         object_name(r.constid),
  14934.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  14935.     from
  14936.         sysobjects o1, sysobjects o2,
  14937.         syscolumns c1, syscolumns c2,
  14938.         sysreferences r, sysindexes i, sysusers u
  14939.     where    
  14940.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  14941.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  14942.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  14943.     and (@pk_table_name is null or @pk_table_name = o1.name)
  14944.     and (@fk_table_name is null or @fk_table_name = o2.name)
  14945.     and    o1.id = r.rkeyid
  14946.     and    r.keycnt >= 2
  14947.     and    o1.id = c1.id
  14948.     and    c1.colid = r.rkey2
  14949.     and r.fkeyid = o2.id
  14950.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  14951.     and    o2.id = c2.id    
  14952.     and    c2.colid = r.fkey2
  14953.     and i.id = r.rkeyid
  14954.     and i.indid = r.rkeyindid
  14955.     and u.uid = user_id()
  14956.     and ( suser_id() = 1     /* User is the System Administrator */
  14957.     or (
  14958.         o1.uid = user_id()     /* User created the object */
  14959.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14960.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14961.             from sysprotects p
  14962.             where p.id = o1.id
  14963.             /* get rows for public,current user,user's group */
  14964.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14965.             /* check for SELECT,EXECUTE privilege */
  14966.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14967.             ) = 1    /* final magic...compare Grants      */
  14968.         )
  14969.     and (
  14970.         o2.uid = user_id()     /* User created the object */
  14971.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  14972.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  14973.             from sysprotects p
  14974.             where p.id = o2.id
  14975.             /* get rows for public,current user,user's group */
  14976.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  14977.             /* check for SELECT,EXECUTE privilege */
  14978.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  14979.             ) = 1    /* final magic...compare Grants      */
  14980.         )
  14981.     )
  14982.     union all
  14983.     select
  14984.         db_name(r.rkeydbid),
  14985.         user_name(o1.uid),
  14986.         o1.name,
  14987.         c1.name,
  14988.         convert(binary(16),null),
  14989.         convert(int,null),
  14990.         db_name(r.fkeydbid),
  14991.         user_name(o2.uid),
  14992.         o2.name,
  14993.         c2.name,
  14994.         convert(binary(16),null),
  14995.         convert(int,null),
  14996.         convert(int,3),
  14997.         'NO ACTION',
  14998.         'NO ACTION',
  14999.         i.name,
  15000.         object_name(r.constid),
  15001.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15002.     from
  15003.         sysobjects o1, sysobjects o2,
  15004.         syscolumns c1, syscolumns c2,
  15005.         sysreferences r, sysindexes i, sysusers u
  15006.     where    
  15007.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15008.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15009.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15010.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15011.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15012.     and    o1.id = r.rkeyid
  15013.     and    r.keycnt >= 3
  15014.     and    o1.id = c1.id
  15015.     and    c1.colid = r.rkey3
  15016.     and r.fkeyid = o2.id
  15017.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15018.     and    o2.id = c2.id    
  15019.     and    c2.colid = r.fkey3
  15020.     and i.id = r.rkeyid
  15021.     and i.indid = r.rkeyindid
  15022.     and u.uid = user_id()
  15023.     and ( suser_id() = 1     /* User is the System Administrator */
  15024.     or (
  15025.         o1.uid = user_id()     /* User created the object */
  15026.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15027.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15028.             from sysprotects p
  15029.             where p.id = o1.id
  15030.             /* get rows for public,current user,user's group */
  15031.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15032.             /* check for SELECT,EXECUTE privilege */
  15033.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15034.             ) = 1    /* final magic...compare Grants      */
  15035.         )
  15036.     and (
  15037.         o2.uid = user_id()     /* User created the object */
  15038.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15039.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15040.             from sysprotects p
  15041.             where p.id = o2.id
  15042.             /* get rows for public,current user,user's group */
  15043.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15044.             /* check for SELECT,EXECUTE privilege */
  15045.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15046.             ) = 1    /* final magic...compare Grants      */
  15047.         )
  15048.     )
  15049.     union all
  15050.     select
  15051.         db_name(r.rkeydbid),
  15052.         user_name(o1.uid),
  15053.         o1.name,
  15054.         c1.name,
  15055.         convert(binary(16),null),
  15056.         convert(int,null),
  15057.         db_name(r.fkeydbid),
  15058.         user_name(o2.uid),
  15059.         o2.name,
  15060.         c2.name,
  15061.         convert(binary(16),null),
  15062.         convert(int,null),
  15063.         convert(int,4),
  15064.         'NO ACTION',
  15065.         'NO ACTION',
  15066.         i.name,
  15067.         object_name(r.constid),
  15068.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15069.     from
  15070.         sysobjects o1, sysobjects o2,
  15071.         syscolumns c1, syscolumns c2,
  15072.         sysreferences r, sysindexes i, sysusers u
  15073.     where    
  15074.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15075.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15076.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15077.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15078.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15079.     and    o1.id = r.rkeyid
  15080.     and    r.keycnt >= 4
  15081.     and    o1.id = c1.id
  15082.     and    c1.colid = r.rkey4
  15083.     and r.fkeyid = o2.id
  15084.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15085.     and    o2.id = c2.id    
  15086.     and    c2.colid = r.fkey4
  15087.     and i.id = r.rkeyid
  15088.     and i.indid = r.rkeyindid
  15089.     and u.uid = user_id()
  15090.     and ( suser_id() = 1     /* User is the System Administrator */
  15091.     or (
  15092.         o1.uid = user_id()     /* User created the object */
  15093.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15094.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15095.             from sysprotects p
  15096.             where p.id = o1.id
  15097.             /* get rows for public,current user,user's group */
  15098.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15099.             /* check for SELECT,EXECUTE privilege */
  15100.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15101.             ) = 1    /* final magic...compare Grants      */
  15102.         )
  15103.     and (
  15104.         o2.uid = user_id()     /* User created the object */
  15105.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15106.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15107.             from sysprotects p
  15108.             where p.id = o2.id
  15109.             /* get rows for public,current user,user's group */
  15110.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15111.             /* check for SELECT,EXECUTE privilege */
  15112.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15113.             ) = 1    /* final magic...compare Grants      */
  15114.         )
  15115.     )
  15116.     union all
  15117.     select
  15118.         db_name(r.rkeydbid),
  15119.         user_name(o1.uid),
  15120.         o1.name,
  15121.         c1.name,
  15122.         convert(binary(16),null),
  15123.         convert(int,null),
  15124.         db_name(r.fkeydbid),
  15125.         user_name(o2.uid),
  15126.         o2.name,
  15127.         c2.name,
  15128.         convert(binary(16),null),
  15129.         convert(int,null),
  15130.         convert(int,5),
  15131.         'NO ACTION',
  15132.         'NO ACTION',
  15133.         i.name,
  15134.         object_name(r.constid),
  15135.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15136.     from
  15137.         sysobjects o1, sysobjects o2,
  15138.         syscolumns c1, syscolumns c2,
  15139.         sysreferences r, sysindexes i, sysusers u
  15140.     where    
  15141.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15142.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15143.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15144.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15145.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15146.     and    o1.id = r.rkeyid
  15147.     and    r.keycnt >= 5
  15148.     and    o1.id = c1.id
  15149.     and    c1.colid = r.rkey5
  15150.     and r.fkeyid = o2.id
  15151.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15152.     and    o2.id = c2.id    
  15153.     and    c2.colid = r.fkey5
  15154.     and i.id = r.rkeyid
  15155.     and i.indid = r.rkeyindid
  15156.     and u.uid = user_id()
  15157.     and ( suser_id() = 1     /* User is the System Administrator */
  15158.     or (
  15159.         o1.uid = user_id()     /* User created the object */
  15160.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15161.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15162.             from sysprotects p
  15163.             where p.id = o1.id
  15164.             /* get rows for public,current user,user's group */
  15165.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15166.             /* check for SELECT,EXECUTE privilege */
  15167.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15168.             ) = 1    /* final magic...compare Grants      */
  15169.         )
  15170.     and (
  15171.         o2.uid = user_id()     /* User created the object */
  15172.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15173.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15174.             from sysprotects p
  15175.             where p.id = o2.id
  15176.             /* get rows for public,current user,user's group */
  15177.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15178.             /* check for SELECT,EXECUTE privilege */
  15179.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15180.             ) = 1    /* final magic...compare Grants      */
  15181.         )
  15182.     )
  15183.     union all
  15184.     select
  15185.         db_name(r.rkeydbid),
  15186.         user_name(o1.uid),
  15187.         o1.name,
  15188.         c1.name,
  15189.         convert(binary(16),null),
  15190.         convert(int,null),
  15191.         db_name(r.fkeydbid),
  15192.         user_name(o2.uid),
  15193.         o2.name,
  15194.         c2.name,
  15195.         convert(binary(16),null),
  15196.         convert(int,null),
  15197.         convert(int,6),
  15198.         'NO ACTION',
  15199.         'NO ACTION',
  15200.         i.name,
  15201.         object_name(r.constid),
  15202.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15203.     from
  15204.         sysobjects o1, sysobjects o2,
  15205.         syscolumns c1, syscolumns c2,
  15206.         sysreferences r, sysindexes i, sysusers u
  15207.     where    
  15208.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15209.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15210.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15211.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15212.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15213.     and    o1.id = r.rkeyid
  15214.     and    r.keycnt >= 6
  15215.     and    o1.id = c1.id
  15216.     and    c1.colid = r.rkey6
  15217.     and r.fkeyid = o2.id
  15218.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15219.     and    o2.id = c2.id    
  15220.     and    c2.colid = r.fkey6
  15221.     and i.id = r.rkeyid
  15222.     and i.indid = r.rkeyindid
  15223.     and u.uid = user_id()
  15224.     and ( suser_id() = 1     /* User is the System Administrator */
  15225.     or (
  15226.         o1.uid = user_id()     /* User created the object */
  15227.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15228.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15229.             from sysprotects p
  15230.             where p.id = o1.id
  15231.             /* get rows for public,current user,user's group */
  15232.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15233.             /* check for SELECT,EXECUTE privilege */
  15234.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15235.             ) = 1    /* final magic...compare Grants      */
  15236.         )
  15237.     and (
  15238.         o2.uid = user_id()     /* User created the object */
  15239.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15240.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15241.             from sysprotects p
  15242.             where p.id = o2.id
  15243.             /* get rows for public,current user,user's group */
  15244.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15245.             /* check for SELECT,EXECUTE privilege */
  15246.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15247.             ) = 1    /* final magic...compare Grants      */
  15248.         )
  15249.     )
  15250.     union all
  15251.     select
  15252.         db_name(r.rkeydbid),
  15253.         user_name(o1.uid),
  15254.         o1.name,
  15255.         c1.name,
  15256.         convert(binary(16),null),
  15257.         convert(int,null),
  15258.         db_name(r.fkeydbid),
  15259.         user_name(o2.uid),
  15260.         o2.name,
  15261.         c2.name,
  15262.         convert(binary(16),null),
  15263.         convert(int,null),
  15264.         convert(int,7),
  15265.         'NO ACTION',
  15266.         'NO ACTION',
  15267.         i.name,
  15268.         object_name(r.constid),
  15269.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15270.     from
  15271.         sysobjects o1, sysobjects o2,
  15272.         syscolumns c1, syscolumns c2,
  15273.         sysreferences r, sysindexes i, sysusers u
  15274.     where    
  15275.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15276.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15277.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15278.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15279.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15280.     and    o1.id = r.rkeyid
  15281.     and    r.keycnt >= 7
  15282.     and    o1.id = c1.id
  15283.     and    c1.colid = r.rkey7
  15284.     and r.fkeyid = o2.id
  15285.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15286.     and    o2.id = c2.id    
  15287.     and    c2.colid = r.fkey7
  15288.     and i.id = r.rkeyid
  15289.     and i.indid = r.rkeyindid
  15290.     and u.uid = user_id()
  15291.     and ( suser_id() = 1     /* User is the System Administrator */
  15292.     or (
  15293.         o1.uid = user_id()     /* User created the object */
  15294.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15295.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15296.             from sysprotects p
  15297.             where p.id = o1.id
  15298.             /* get rows for public,current user,user's group */
  15299.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15300.             /* check for SELECT,EXECUTE privilege */
  15301.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15302.             ) = 1    /* final magic...compare Grants      */
  15303.         )
  15304.     and (
  15305.         o2.uid = user_id()     /* User created the object */
  15306.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15307.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15308.             from sysprotects p
  15309.             where p.id = o2.id
  15310.             /* get rows for public,current user,user's group */
  15311.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15312.             /* check for SELECT,EXECUTE privilege */
  15313.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15314.             ) = 1    /* final magic...compare Grants      */
  15315.         )
  15316.     )
  15317.     union all
  15318.     select
  15319.         db_name(r.rkeydbid),
  15320.         user_name(o1.uid),
  15321.         o1.name,
  15322.         c1.name,
  15323.         convert(binary(16),null),
  15324.         convert(int,null),
  15325.         db_name(r.fkeydbid),
  15326.         user_name(o2.uid),
  15327.         o2.name,
  15328.         c2.name,
  15329.         convert(binary(16),null),
  15330.         convert(int,null),
  15331.         convert(int,8),
  15332.         'NO ACTION',
  15333.         'NO ACTION',
  15334.         i.name,
  15335.         object_name(r.constid),
  15336.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15337.     from
  15338.         sysobjects o1, sysobjects o2,
  15339.         syscolumns c1, syscolumns c2,
  15340.         sysreferences r, sysindexes i, sysusers u
  15341.     where    
  15342.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15343.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15344.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15345.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15346.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15347.     and    o1.id = r.rkeyid
  15348.     and    r.keycnt >= 8
  15349.     and    o1.id = c1.id
  15350.     and    c1.colid = r.rkey8
  15351.     and r.fkeyid = o2.id
  15352.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15353.     and    o2.id = c2.id    
  15354.     and    c2.colid = r.fkey8
  15355.     and i.id = r.rkeyid
  15356.     and i.indid = r.rkeyindid
  15357.     and u.uid = user_id()
  15358.     and ( suser_id() = 1     /* User is the System Administrator */
  15359.     or (
  15360.         o1.uid = user_id()     /* User created the object */
  15361.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15362.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15363.             from sysprotects p
  15364.             where p.id = o1.id
  15365.             /* get rows for public,current user,user's group */
  15366.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15367.             /* check for SELECT,EXECUTE privilege */
  15368.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15369.             ) = 1    /* final magic...compare Grants      */
  15370.         )
  15371.     and (
  15372.         o2.uid = user_id()     /* User created the object */
  15373.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15374.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15375.             from sysprotects p
  15376.             where p.id = o2.id
  15377.             /* get rows for public,current user,user's group */
  15378.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15379.             /* check for SELECT,EXECUTE privilege */
  15380.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15381.             ) = 1    /* final magic...compare Grants      */
  15382.         )
  15383.     )
  15384.     union all
  15385.     select
  15386.         db_name(r.rkeydbid),
  15387.         user_name(o1.uid),
  15388.         o1.name,
  15389.         c1.name,
  15390.         convert(binary(16),null),
  15391.         convert(int,null),
  15392.         db_name(r.fkeydbid),
  15393.         user_name(o2.uid),
  15394.         o2.name,
  15395.         c2.name,
  15396.         convert(binary(16),null),
  15397.         convert(int,null),
  15398.         convert(int,9),
  15399.         'NO ACTION',
  15400.         'NO ACTION',
  15401.         i.name,
  15402.         object_name(r.constid),
  15403.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15404.     from
  15405.         sysobjects o1, sysobjects o2,
  15406.         syscolumns c1, syscolumns c2,
  15407.         sysreferences r, sysindexes i, sysusers u
  15408.     where    
  15409.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15410.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15411.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15412.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15413.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15414.     and    o1.id = r.rkeyid
  15415.     and    r.keycnt >= 9
  15416.     and    o1.id = c1.id
  15417.     and    c1.colid = r.rkey9
  15418.     and r.fkeyid = o2.id
  15419.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15420.     and    o2.id = c2.id    
  15421.     and    c2.colid = r.fkey9
  15422.     and i.id = r.rkeyid
  15423.     and i.indid = r.rkeyindid
  15424.     and u.uid = user_id()
  15425.     and ( suser_id() = 1     /* User is the System Administrator */
  15426.     or (
  15427.         o1.uid = user_id()     /* User created the object */
  15428.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15429.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15430.             from sysprotects p
  15431.             where p.id = o1.id
  15432.             /* get rows for public,current user,user's group */
  15433.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15434.             /* check for SELECT,EXECUTE privilege */
  15435.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15436.             ) = 1    /* final magic...compare Grants      */
  15437.         )
  15438.     and (
  15439.         o2.uid = user_id()     /* User created the object */
  15440.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15441.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15442.             from sysprotects p
  15443.             where p.id = o2.id
  15444.             /* get rows for public,current user,user's group */
  15445.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15446.             /* check for SELECT,EXECUTE privilege */
  15447.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15448.             ) = 1    /* final magic...compare Grants      */
  15449.         )
  15450.     )
  15451.     union all
  15452.     select
  15453.         db_name(r.rkeydbid),
  15454.         user_name(o1.uid),
  15455.         o1.name,
  15456.         c1.name,
  15457.         convert(binary(16),null),
  15458.         convert(int,null),
  15459.         db_name(r.fkeydbid),
  15460.         user_name(o2.uid),
  15461.         o2.name,
  15462.         c2.name,
  15463.         convert(binary(16),null),
  15464.         convert(int,null),
  15465.         convert(int,10),
  15466.         'NO ACTION',
  15467.         'NO ACTION',
  15468.         i.name,
  15469.         object_name(r.constid),
  15470.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15471.     from
  15472.         sysobjects o1, sysobjects o2,
  15473.         syscolumns c1, syscolumns c2,
  15474.         sysreferences r, sysindexes i, sysusers u
  15475.     where    
  15476.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15477.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15478.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15479.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15480.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15481.     and    o1.id = r.rkeyid
  15482.     and    r.keycnt >= 10
  15483.     and    o1.id = c1.id
  15484.     and    c1.colid = r.rkey10
  15485.     and r.fkeyid = o2.id
  15486.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15487.     and    o2.id = c2.id    
  15488.     and    c2.colid = r.fkey10
  15489.     and i.id = r.rkeyid
  15490.     and i.indid = r.rkeyindid
  15491.     and u.uid = user_id()
  15492.     and ( suser_id() = 1     /* User is the System Administrator */
  15493.     or (
  15494.         o1.uid = user_id()     /* User created the object */
  15495.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15496.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15497.             from sysprotects p
  15498.             where p.id = o1.id
  15499.             /* get rows for public,current user,user's group */
  15500.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15501.             /* check for SELECT,EXECUTE privilege */
  15502.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15503.             ) = 1    /* final magic...compare Grants      */
  15504.         )
  15505.     and (
  15506.         o2.uid = user_id()     /* User created the object */
  15507.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15508.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15509.             from sysprotects p
  15510.             where p.id = o2.id
  15511.             /* get rows for public,current user,user's group */
  15512.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15513.             /* check for SELECT,EXECUTE privilege */
  15514.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15515.             ) = 1    /* final magic...compare Grants      */
  15516.         )
  15517.     )
  15518.     union all
  15519.     select
  15520.         db_name(r.rkeydbid),
  15521.         user_name(o1.uid),
  15522.         o1.name,
  15523.         c1.name,
  15524.         convert(binary(16),null),
  15525.         convert(int,null),
  15526.         db_name(r.fkeydbid),
  15527.         user_name(o2.uid),
  15528.         o2.name,
  15529.         c2.name,
  15530.         convert(binary(16),null),
  15531.         convert(int,null),
  15532.         convert(int,11),
  15533.         'NO ACTION',
  15534.         'NO ACTION',
  15535.         i.name,
  15536.         object_name(r.constid),
  15537.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15538.     from
  15539.         sysobjects o1, sysobjects o2,
  15540.         syscolumns c1, syscolumns c2,
  15541.         sysreferences r, sysindexes i, sysusers u
  15542.     where    
  15543.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15544.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15545.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15546.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15547.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15548.     and    o1.id = r.rkeyid
  15549.     and    r.keycnt >= 11
  15550.     and    o1.id = c1.id
  15551.     and    c1.colid = r.rkey11
  15552.     and r.fkeyid = o2.id
  15553.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15554.     and    o2.id = c2.id    
  15555.     and    c2.colid = r.fkey11
  15556.     and i.id = r.rkeyid
  15557.     and i.indid = r.rkeyindid
  15558.     and u.uid = user_id()
  15559.     and ( suser_id() = 1     /* User is the System Administrator */
  15560.     or (
  15561.         o1.uid = user_id()     /* User created the object */
  15562.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15563.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15564.             from sysprotects p
  15565.             where p.id = o1.id
  15566.             /* get rows for public,current user,user's group */
  15567.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15568.             /* check for SELECT,EXECUTE privilege */
  15569.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15570.             ) = 1    /* final magic...compare Grants      */
  15571.         )
  15572.     and (
  15573.         o2.uid = user_id()     /* User created the object */
  15574.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15575.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15576.             from sysprotects p
  15577.             where p.id = o2.id
  15578.             /* get rows for public,current user,user's group */
  15579.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15580.             /* check for SELECT,EXECUTE privilege */
  15581.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15582.             ) = 1    /* final magic...compare Grants      */
  15583.         )
  15584.     )
  15585.     union all
  15586.     select
  15587.         db_name(r.rkeydbid),
  15588.         user_name(o1.uid),
  15589.         o1.name,
  15590.         c1.name,
  15591.         convert(binary(16),null),
  15592.         convert(int,null),
  15593.         db_name(r.fkeydbid),
  15594.         user_name(o2.uid),
  15595.         o2.name,
  15596.         c2.name,
  15597.         convert(binary(16),null),
  15598.         convert(int,null),
  15599.         convert(int,12),
  15600.         'NO ACTION',
  15601.         'NO ACTION',
  15602.         i.name,
  15603.         object_name(r.constid),
  15604.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15605.     from
  15606.         sysobjects o1, sysobjects o2,
  15607.         syscolumns c1, syscolumns c2,
  15608.         sysreferences r, sysindexes i, sysusers u
  15609.     where    
  15610.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15611.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15612.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15613.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15614.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15615.     and    o1.id = r.rkeyid
  15616.     and    r.keycnt >= 12
  15617.     and    o1.id = c1.id
  15618.     and    c1.colid = r.rkey12
  15619.     and r.fkeyid = o2.id
  15620.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15621.     and    o2.id = c2.id    
  15622.     and    c2.colid = r.fkey12
  15623.     and i.id = r.rkeyid
  15624.     and i.indid = r.rkeyindid
  15625.     and u.uid = user_id()
  15626.     and ( suser_id() = 1     /* User is the System Administrator */
  15627.     or (
  15628.         o1.uid = user_id()     /* User created the object */
  15629.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15630.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15631.             from sysprotects p
  15632.             where p.id = o1.id
  15633.             /* get rows for public,current user,user's group */
  15634.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15635.             /* check for SELECT,EXECUTE privilege */
  15636.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15637.             ) = 1    /* final magic...compare Grants      */
  15638.         )
  15639.     and (
  15640.         o2.uid = user_id()     /* User created the object */
  15641.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15642.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15643.             from sysprotects p
  15644.             where p.id = o2.id
  15645.             /* get rows for public,current user,user's group */
  15646.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15647.             /* check for SELECT,EXECUTE privilege */
  15648.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15649.             ) = 1    /* final magic...compare Grants      */
  15650.         )
  15651.     )
  15652.     union all
  15653.     select
  15654.         db_name(r.rkeydbid),
  15655.         user_name(o1.uid),
  15656.         o1.name,
  15657.         c1.name,
  15658.         convert(binary(16),null),
  15659.         convert(int,null),
  15660.         db_name(r.fkeydbid),
  15661.         user_name(o2.uid),
  15662.         o2.name,
  15663.         c2.name,
  15664.         convert(binary(16),null),
  15665.         convert(int,null),
  15666.         convert(int,13),
  15667.         'NO ACTION',
  15668.         'NO ACTION',
  15669.         i.name,
  15670.         object_name(r.constid),
  15671.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15672.     from
  15673.         sysobjects o1, sysobjects o2,
  15674.         syscolumns c1, syscolumns c2,
  15675.         sysreferences r, sysindexes i, sysusers u
  15676.     where    
  15677.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15678.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15679.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15680.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15681.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15682.     and    o1.id = r.rkeyid
  15683.     and    r.keycnt >= 13
  15684.     and    o1.id = c1.id
  15685.     and    c1.colid = r.rkey13
  15686.     and r.fkeyid = o2.id
  15687.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15688.     and    o2.id = c2.id    
  15689.     and    c2.colid = r.fkey13
  15690.     and i.id = r.rkeyid
  15691.     and i.indid = r.rkeyindid
  15692.     and u.uid = user_id()
  15693.     and ( suser_id() = 1     /* User is the System Administrator */
  15694.     or (
  15695.         o1.uid = user_id()     /* User created the object */
  15696.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15697.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15698.             from sysprotects p
  15699.             where p.id = o1.id
  15700.             /* get rows for public,current user,user's group */
  15701.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15702.             /* check for SELECT,EXECUTE privilege */
  15703.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15704.             ) = 1    /* final magic...compare Grants      */
  15705.         )
  15706.     and (
  15707.         o2.uid = user_id()     /* User created the object */
  15708.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15709.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15710.             from sysprotects p
  15711.             where p.id = o2.id
  15712.             /* get rows for public,current user,user's group */
  15713.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15714.             /* check for SELECT,EXECUTE privilege */
  15715.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15716.             ) = 1    /* final magic...compare Grants      */
  15717.         )
  15718.     )
  15719.     union all
  15720.     select
  15721.         db_name(r.rkeydbid),
  15722.         user_name(o1.uid),
  15723.         o1.name,
  15724.         c1.name,
  15725.         convert(binary(16),null),
  15726.         convert(int,null),
  15727.         db_name(r.fkeydbid),
  15728.         user_name(o2.uid),
  15729.         o2.name,
  15730.         c2.name,
  15731.         convert(binary(16),null),
  15732.         convert(int,null),
  15733.         convert(int,14),
  15734.         'NO ACTION',
  15735.         'NO ACTION',
  15736.         i.name,
  15737.         object_name(r.constid),
  15738.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15739.     from
  15740.         sysobjects o1, sysobjects o2,
  15741.         syscolumns c1, syscolumns c2,
  15742.         sysreferences r, sysindexes i, sysusers u
  15743.     where    
  15744.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15745.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15746.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15747.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15748.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15749.     and    o1.id = r.rkeyid
  15750.     and    r.keycnt >= 14
  15751.     and    o1.id = c1.id
  15752.     and    c1.colid = r.rkey14
  15753.     and r.fkeyid = o2.id
  15754.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15755.     and    o2.id = c2.id    
  15756.     and    c2.colid = r.fkey14
  15757.     and i.id = r.rkeyid
  15758.     and i.indid = r.rkeyindid
  15759.     and u.uid = user_id()
  15760.     and ( suser_id() = 1     /* User is the System Administrator */
  15761.     or (
  15762.         o1.uid = user_id()     /* User created the object */
  15763.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15764.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15765.             from sysprotects p
  15766.             where p.id = o1.id
  15767.             /* get rows for public,current user,user's group */
  15768.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15769.             /* check for SELECT,EXECUTE privilege */
  15770.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15771.             ) = 1    /* final magic...compare Grants      */
  15772.         )
  15773.     and (
  15774.         o2.uid = user_id()     /* User created the object */
  15775.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15776.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15777.             from sysprotects p
  15778.             where p.id = o2.id
  15779.             /* get rows for public,current user,user's group */
  15780.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15781.             /* check for SELECT,EXECUTE privilege */
  15782.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15783.             ) = 1    /* final magic...compare Grants      */
  15784.         )
  15785.     )
  15786.     union all
  15787.     select
  15788.         db_name(r.rkeydbid),
  15789.         user_name(o1.uid),
  15790.         o1.name,
  15791.         c1.name,
  15792.         convert(binary(16),null),
  15793.         convert(int,null),
  15794.         db_name(r.fkeydbid),
  15795.         user_name(o2.uid),
  15796.         o2.name,
  15797.         c2.name,
  15798.         convert(binary(16),null),
  15799.         convert(int,null),
  15800.         convert(int,15),
  15801.         'NO ACTION',
  15802.         'NO ACTION',
  15803.         i.name,
  15804.         object_name(r.constid),
  15805.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15806.     from
  15807.         sysobjects o1, sysobjects o2,
  15808.         syscolumns c1, syscolumns c2,
  15809.         sysreferences r, sysindexes i, sysusers u
  15810.     where    
  15811.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15812.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15813.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15814.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15815.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15816.     and    o1.id = r.rkeyid
  15817.     and    r.keycnt >= 15
  15818.     and    o1.id = c1.id
  15819.     and    c1.colid = r.rkey15
  15820.     and r.fkeyid = o2.id
  15821.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15822.     and    o2.id = c2.id    
  15823.     and    c2.colid = r.fkey15
  15824.     and i.id = r.rkeyid
  15825.     and i.indid = r.rkeyindid
  15826.     and u.uid = user_id()
  15827.     and ( suser_id() = 1     /* User is the System Administrator */
  15828.     or (
  15829.         o1.uid = user_id()     /* User created the object */
  15830.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15831.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15832.             from sysprotects p
  15833.             where p.id = o1.id
  15834.             /* get rows for public,current user,user's group */
  15835.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15836.             /* check for SELECT,EXECUTE privilege */
  15837.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15838.             ) = 1    /* final magic...compare Grants      */
  15839.         )
  15840.     and (
  15841.         o2.uid = user_id()     /* User created the object */
  15842.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15843.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15844.             from sysprotects p
  15845.             where p.id = o2.id
  15846.             /* get rows for public,current user,user's group */
  15847.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15848.             /* check for SELECT,EXECUTE privilege */
  15849.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15850.             ) = 1    /* final magic...compare Grants      */
  15851.         )
  15852.     )
  15853.     union all
  15854.     select
  15855.         db_name(r.rkeydbid),
  15856.         user_name(o1.uid),
  15857.         o1.name,
  15858.         c1.name,
  15859.         convert(binary(16),null),
  15860.         convert(int,null),
  15861.         db_name(r.fkeydbid),
  15862.         user_name(o2.uid),
  15863.         o2.name,
  15864.         c2.name,
  15865.         convert(binary(16),null),
  15866.         convert(int,null),
  15867.         convert(int,16),
  15868.         'NO ACTION',
  15869.         'NO ACTION',
  15870.         i.name,
  15871.         object_name(r.constid),
  15872.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15873.     from
  15874.         sysobjects o1, sysobjects o2,
  15875.         syscolumns c1, syscolumns c2,
  15876.         sysreferences r, sysindexes i, sysusers u
  15877.     where    
  15878.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15879.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15880.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15881.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15882.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15883.     and    o1.id = r.rkeyid
  15884.     and    o1.id = c1.id
  15885.     and    r.keycnt >= 16
  15886.     and    c1.colid = r.rkey16
  15887.     and r.fkeyid = o2.id
  15888.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  15889.     and    o2.id = c2.id    
  15890.     and    c2.colid = r.fkey16
  15891.     and i.id = r.rkeyid
  15892.     and i.indid = r.rkeyindid
  15893.     and u.uid = user_id()
  15894.     and ( suser_id() = 1     /* User is the System Administrator */
  15895.     or (
  15896.         o1.uid = user_id()     /* User created the object */
  15897.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15898.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15899.             from sysprotects p
  15900.             where p.id = o1.id
  15901.             /* get rows for public,current user,user's group */
  15902.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15903.             /* check for SELECT,EXECUTE privilege */
  15904.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15905.             ) = 1    /* final magic...compare Grants      */
  15906.         )
  15907.     and (
  15908.         o2.uid = user_id()     /* User created the object */
  15909.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  15910.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  15911.             from sysprotects p
  15912.             where p.id = o2.id
  15913.             /* get rows for public,current user,user's group */
  15914.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  15915.             /* check for SELECT,EXECUTE privilege */
  15916.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  15917.             ) = 1    /* final magic...compare Grants      */
  15918.         )
  15919.     )
  15920.     order by 8,9,2,3,13
  15921.     END
  15922. go
  15923. dump tran master with no_log
  15924. go
  15925. CREATE PROCEDURE sp_foreign_keys_rowset;2
  15926.     (
  15927.        @handle            int output,
  15928.        @scrollopt        int output,
  15929.     @ccopt            int output,
  15930.     @rows            int output,
  15931.      @pk_table_name        varchar(255) = null,
  15932.     @pk_table_schema    varchar(255) = null,
  15933.     @pk_table_catalog    varchar(255) = null,
  15934.     @fk_table_name        varchar(255) = null,
  15935.     @fk_table_schema    varchar(255) = null,
  15936.     @fk_table_catalog    varchar(255) = null
  15937.     )
  15938. as
  15939.     declare @ret int
  15940.     
  15941.     SET NOCOUNT ON
  15942.  
  15943.     create table #spfkeysrowset1
  15944.         (
  15945.         PK_TABLE_CATALOG    sysname not null,
  15946.         PK_TABLE_SCHEMA        sysname not null,
  15947.         PK_TABLE_NAME         sysname not null,
  15948.         PK_COLUMN_NAME         sysname not null,
  15949.         PK_COLUMN_GUID        binary(16) null,
  15950.         PK_COLUMN_PROPID    int null,
  15951.         FK_TABLE_CATALOG    sysname not null,
  15952.         FK_TABLE_SCHEMA        sysname not null,
  15953.         FK_TABLE_NAME         sysname not null,
  15954.         FK_COLUMN_NAME         sysname not null,
  15955.         FK_COLUMN_GUID        binary(16) null,
  15956.         FK_COLUMN_PROPID    int null,
  15957.         ORDINAL                int not null,
  15958.         UPDATE_RULE            sysname not null,
  15959.         DELETE_RULE         sysname not null,
  15960.         PK_NAME                sysname not null,
  15961.         FK_NAME                sysname not null,
  15962.         DEFERRABILITY        smallint not null        
  15963.         )
  15964.     BEGIN
  15965.     insert into #spfkeysrowset1
  15966.     select
  15967.         db_name(r.rkeydbid),
  15968.         user_name(o1.uid),
  15969.         o1.name,
  15970.         c1.name,
  15971.         convert(binary(16),null),
  15972.         convert(int,null),
  15973.         db_name(r.fkeydbid),
  15974.         user_name(o2.uid),
  15975.         o2.name,
  15976.         c2.name,
  15977.         convert(binary(16),null),
  15978.         convert(int,null),
  15979.         convert(int,1),
  15980.         'NO ACTION',
  15981.         'NO ACTION',
  15982.         i.name,
  15983.         object_name(r.constid),
  15984.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  15985.     from
  15986.         sysobjects o1, sysobjects o2,
  15987.         syscolumns c1, syscolumns c2,
  15988.         sysreferences r, sysindexes i, sysusers u
  15989.     where    
  15990.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  15991.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  15992.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  15993.     and (@pk_table_name is null or @pk_table_name = o1.name)
  15994.     and (@fk_table_name is null or @fk_table_name = o2.name)
  15995.     and    o1.id = r.rkeyid
  15996.     and    o1.id = c1.id
  15997.     and    c1.colid = r.rkey1
  15998.     and r.fkeyid = o2.id
  15999.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16000.     and    o2.id = c2.id    
  16001.     and    c2.colid = r.fkey1
  16002.     and i.id = r.rkeyid
  16003.     and i.indid = r.rkeyindid
  16004.     and u.uid = user_id()
  16005.     and ( suser_id() = 1     /* User is the System Administrator */
  16006.     or (
  16007.         o1.uid = user_id()     /* User created the object */
  16008.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16009.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16010.             from sysprotects p
  16011.             where p.id = o1.id
  16012.             /* get rows for public,current user,user's group */
  16013.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16014.             /* check for SELECT,EXECUTE privilege */
  16015.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16016.             ) = 1    /* final magic...compare Grants      */
  16017.         )
  16018.     and (
  16019.         o2.uid = user_id()     /* User created the object */
  16020.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16021.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16022.             from sysprotects p
  16023.             where p.id = o2.id
  16024.             /* get rows for public,current user,user's group */
  16025.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16026.             /* check for SELECT,EXECUTE privilege */
  16027.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16028.             ) = 1    /* final magic...compare Grants      */
  16029.         )
  16030.     )    
  16031.     union all
  16032.     select
  16033.         db_name(r.rkeydbid),
  16034.         user_name(o1.uid),
  16035.         o1.name,
  16036.         c1.name,
  16037.         convert(binary(16),null),
  16038.         convert(int,null),
  16039.         db_name(r.fkeydbid),
  16040.         user_name(o2.uid),
  16041.         o2.name,
  16042.         c2.name,
  16043.         convert(binary(16),null),
  16044.         convert(int,null),
  16045.         convert(int,2),
  16046.         'NO ACTION',
  16047.         'NO ACTION',
  16048.         i.name,
  16049.         object_name(r.constid),
  16050.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16051.     from
  16052.         sysobjects o1, sysobjects o2,
  16053.         syscolumns c1, syscolumns c2,
  16054.         sysreferences r, sysindexes i, sysusers u
  16055.     where    
  16056.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16057.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16058.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16059.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16060.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16061.     and    o1.id = r.rkeyid
  16062.     and    r.keycnt >= 2
  16063.     and    o1.id = c1.id
  16064.     and    c1.colid = r.rkey2
  16065.     and r.fkeyid = o2.id
  16066.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16067.     and    o2.id = c2.id    
  16068.     and    c2.colid = r.fkey2
  16069.     and i.id = r.rkeyid
  16070.     and i.indid = r.rkeyindid
  16071.     and u.uid = user_id()
  16072.     and ( suser_id() = 1     /* User is the System Administrator */
  16073.     or (
  16074.         o1.uid = user_id()     /* User created the object */
  16075.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16076.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16077.             from sysprotects p
  16078.             where p.id = o1.id
  16079.             /* get rows for public,current user,user's group */
  16080.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16081.             /* check for SELECT,EXECUTE privilege */
  16082.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16083.             ) = 1    /* final magic...compare Grants      */
  16084.         )
  16085.     and (
  16086.         o2.uid = user_id()     /* User created the object */
  16087.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16088.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16089.             from sysprotects p
  16090.             where p.id = o2.id
  16091.             /* get rows for public,current user,user's group */
  16092.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16093.             /* check for SELECT,EXECUTE privilege */
  16094.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16095.             ) = 1    /* final magic...compare Grants      */
  16096.         )
  16097.     )    
  16098.     union all
  16099.     select
  16100.         db_name(r.rkeydbid),
  16101.         user_name(o1.uid),
  16102.         o1.name,
  16103.         c1.name,
  16104.         convert(binary(16),null),
  16105.         convert(int,null),
  16106.         db_name(r.fkeydbid),
  16107.         user_name(o2.uid),
  16108.         o2.name,
  16109.         c2.name,
  16110.         convert(binary(16),null),
  16111.         convert(int,null),
  16112.         convert(int,3),
  16113.         'NO ACTION',
  16114.         'NO ACTION',
  16115.         i.name,
  16116.         object_name(r.constid),
  16117.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16118.     from
  16119.         sysobjects o1, sysobjects o2,
  16120.         syscolumns c1, syscolumns c2,
  16121.         sysreferences r, sysindexes i, sysusers u
  16122.     where    
  16123.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16124.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16125.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16126.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16127.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16128.     and    o1.id = r.rkeyid
  16129.     and    r.keycnt >= 3
  16130.     and    o1.id = c1.id
  16131.     and    c1.colid = r.rkey3
  16132.     and r.fkeyid = o2.id
  16133.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16134.     and    o2.id = c2.id    
  16135.     and    c2.colid = r.fkey3
  16136.     and i.id = r.rkeyid
  16137.     and i.indid = r.rkeyindid
  16138.     and u.uid = user_id()
  16139.     and ( suser_id() = 1     /* User is the System Administrator */
  16140.     or (
  16141.         o1.uid = user_id()     /* User created the object */
  16142.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16143.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16144.             from sysprotects p
  16145.             where p.id = o1.id
  16146.             /* get rows for public,current user,user's group */
  16147.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16148.             /* check for SELECT,EXECUTE privilege */
  16149.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16150.             ) = 1    /* final magic...compare Grants      */
  16151.         )
  16152.     and (
  16153.         o2.uid = user_id()     /* User created the object */
  16154.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16155.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16156.             from sysprotects p
  16157.             where p.id = o2.id
  16158.             /* get rows for public,current user,user's group */
  16159.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16160.             /* check for SELECT,EXECUTE privilege */
  16161.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16162.             ) = 1    /* final magic...compare Grants      */
  16163.         )
  16164.     )    
  16165.     union all
  16166.     select
  16167.         db_name(r.rkeydbid),
  16168.         user_name(o1.uid),
  16169.         o1.name,
  16170.         c1.name,
  16171.         convert(binary(16),null),
  16172.         convert(int,null),
  16173.         db_name(r.fkeydbid),
  16174.         user_name(o2.uid),
  16175.         o2.name,
  16176.         c2.name,
  16177.         convert(binary(16),null),
  16178.         convert(int,null),
  16179.         convert(int,4),
  16180.         'NO ACTION',
  16181.         'NO ACTION',
  16182.         i.name,
  16183.         object_name(r.constid),
  16184.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16185.     from
  16186.         sysobjects o1, sysobjects o2,
  16187.         syscolumns c1, syscolumns c2,
  16188.         sysreferences r, sysindexes i, sysusers u
  16189.     where    
  16190.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16191.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16192.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16193.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16194.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16195.     and    o1.id = r.rkeyid
  16196.     and    r.keycnt >= 4
  16197.     and    o1.id = c1.id
  16198.     and    c1.colid = r.rkey4
  16199.     and r.fkeyid = o2.id
  16200.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16201.     and    o2.id = c2.id    
  16202.     and    c2.colid = r.fkey4
  16203.     and i.id = r.rkeyid
  16204.     and i.indid = r.rkeyindid
  16205.     and u.uid = user_id()
  16206.     and ( suser_id() = 1     /* User is the System Administrator */
  16207.     or (
  16208.         o1.uid = user_id()     /* User created the object */
  16209.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16210.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16211.             from sysprotects p
  16212.             where p.id = o1.id
  16213.             /* get rows for public,current user,user's group */
  16214.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16215.             /* check for SELECT,EXECUTE privilege */
  16216.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16217.             ) = 1    /* final magic...compare Grants      */
  16218.         )
  16219.     and (
  16220.         o2.uid = user_id()     /* User created the object */
  16221.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16222.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16223.             from sysprotects p
  16224.             where p.id = o2.id
  16225.             /* get rows for public,current user,user's group */
  16226.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16227.             /* check for SELECT,EXECUTE privilege */
  16228.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16229.             ) = 1    /* final magic...compare Grants      */
  16230.         )
  16231.     )    
  16232.     union all
  16233.     select
  16234.         db_name(r.rkeydbid),
  16235.         user_name(o1.uid),
  16236.         o1.name,
  16237.         c1.name,
  16238.         convert(binary(16),null),
  16239.         convert(int,null),
  16240.         db_name(r.fkeydbid),
  16241.         user_name(o2.uid),
  16242.         o2.name,
  16243.         c2.name,
  16244.         convert(binary(16),null),
  16245.         convert(int,null),
  16246.         convert(int,5),
  16247.         'NO ACTION',
  16248.         'NO ACTION',
  16249.         i.name,
  16250.         object_name(r.constid),
  16251.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16252.     from
  16253.         sysobjects o1, sysobjects o2,
  16254.         syscolumns c1, syscolumns c2,
  16255.         sysreferences r, sysindexes i, sysusers u
  16256.     where    
  16257.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16258.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16259.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16260.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16261.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16262.     and    o1.id = r.rkeyid
  16263.     and    r.keycnt >= 5
  16264.     and    o1.id = c1.id
  16265.     and    c1.colid = r.rkey5
  16266.     and r.fkeyid = o2.id
  16267.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16268.     and    o2.id = c2.id    
  16269.     and    c2.colid = r.fkey5
  16270.     and i.id = r.rkeyid
  16271.     and i.indid = r.rkeyindid
  16272.     and u.uid = user_id()
  16273.     and ( suser_id() = 1     /* User is the System Administrator */
  16274.     or (
  16275.         o1.uid = user_id()     /* User created the object */
  16276.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16277.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16278.             from sysprotects p
  16279.             where p.id = o1.id
  16280.             /* get rows for public,current user,user's group */
  16281.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16282.             /* check for SELECT,EXECUTE privilege */
  16283.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16284.             ) = 1    /* final magic...compare Grants      */
  16285.         )
  16286.     and (
  16287.         o2.uid = user_id()     /* User created the object */
  16288.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16289.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16290.             from sysprotects p
  16291.             where p.id = o2.id
  16292.             /* get rows for public,current user,user's group */
  16293.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16294.             /* check for SELECT,EXECUTE privilege */
  16295.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16296.             ) = 1    /* final magic...compare Grants      */
  16297.         )
  16298.     )    
  16299.     union all
  16300.     select
  16301.         db_name(r.rkeydbid),
  16302.         user_name(o1.uid),
  16303.         o1.name,
  16304.         c1.name,
  16305.         convert(binary(16),null),
  16306.         convert(int,null),
  16307.         db_name(r.fkeydbid),
  16308.         user_name(o2.uid),
  16309.         o2.name,
  16310.         c2.name,
  16311.         convert(binary(16),null),
  16312.         convert(int,null),
  16313.         convert(int,6),
  16314.         'NO ACTION',
  16315.         'NO ACTION',
  16316.         i.name,
  16317.         object_name(r.constid),
  16318.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16319.     from
  16320.         sysobjects o1, sysobjects o2,
  16321.         syscolumns c1, syscolumns c2,
  16322.         sysreferences r, sysindexes i, sysusers u
  16323.     where    
  16324.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16325.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16326.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16327.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16328.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16329.     and    o1.id = r.rkeyid
  16330.     and    r.keycnt >= 6
  16331.     and    o1.id = c1.id
  16332.     and    c1.colid = r.rkey6
  16333.     and r.fkeyid = o2.id
  16334.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16335.     and    o2.id = c2.id    
  16336.     and    c2.colid = r.fkey6
  16337.     and i.id = r.rkeyid
  16338.     and i.indid = r.rkeyindid
  16339.     and u.uid = user_id()
  16340.     and ( suser_id() = 1     /* User is the System Administrator */
  16341.     or (
  16342.         o1.uid = user_id()     /* User created the object */
  16343.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16344.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16345.             from sysprotects p
  16346.             where p.id = o1.id
  16347.             /* get rows for public,current user,user's group */
  16348.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16349.             /* check for SELECT,EXECUTE privilege */
  16350.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16351.             ) = 1    /* final magic...compare Grants      */
  16352.         )
  16353.     and (
  16354.         o2.uid = user_id()     /* User created the object */
  16355.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16356.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16357.             from sysprotects p
  16358.             where p.id = o2.id
  16359.             /* get rows for public,current user,user's group */
  16360.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16361.             /* check for SELECT,EXECUTE privilege */
  16362.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16363.             ) = 1    /* final magic...compare Grants      */
  16364.         )
  16365.     )    
  16366.     union all
  16367.     select
  16368.         db_name(r.rkeydbid),
  16369.         user_name(o1.uid),
  16370.         o1.name,
  16371.         c1.name,
  16372.         convert(binary(16),null),
  16373.         convert(int,null),
  16374.         db_name(r.fkeydbid),
  16375.         user_name(o2.uid),
  16376.         o2.name,
  16377.         c2.name,
  16378.         convert(binary(16),null),
  16379.         convert(int,null),
  16380.         convert(int,7),
  16381.         'NO ACTION',
  16382.         'NO ACTION',
  16383.         i.name,
  16384.         object_name(r.constid),
  16385.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16386.     from
  16387.         sysobjects o1, sysobjects o2,
  16388.         syscolumns c1, syscolumns c2,
  16389.         sysreferences r, sysindexes i, sysusers u
  16390.     where    
  16391.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16392.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16393.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16394.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16395.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16396.     and    o1.id = r.rkeyid
  16397.     and    r.keycnt >= 7
  16398.     and    o1.id = c1.id
  16399.     and    c1.colid = r.rkey7
  16400.     and r.fkeyid = o2.id
  16401.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16402.     and    o2.id = c2.id    
  16403.     and    c2.colid = r.fkey7
  16404.     and i.id = r.rkeyid
  16405.     and i.indid = r.rkeyindid
  16406.     and u.uid = user_id()
  16407.     and ( suser_id() = 1     /* User is the System Administrator */
  16408.     or (
  16409.         o1.uid = user_id()     /* User created the object */
  16410.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16411.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16412.             from sysprotects p
  16413.             where p.id = o1.id
  16414.             /* get rows for public,current user,user's group */
  16415.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16416.             /* check for SELECT,EXECUTE privilege */
  16417.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16418.             ) = 1    /* final magic...compare Grants      */
  16419.         )
  16420.     and (
  16421.         o2.uid = user_id()     /* User created the object */
  16422.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16423.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16424.             from sysprotects p
  16425.             where p.id = o2.id
  16426.             /* get rows for public,current user,user's group */
  16427.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16428.             /* check for SELECT,EXECUTE privilege */
  16429.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16430.             ) = 1    /* final magic...compare Grants      */
  16431.         )
  16432.     )    
  16433.     union all
  16434.     select
  16435.         db_name(r.rkeydbid),
  16436.         user_name(o1.uid),
  16437.         o1.name,
  16438.         c1.name,
  16439.         convert(binary(16),null),
  16440.         convert(int,null),
  16441.         db_name(r.fkeydbid),
  16442.         user_name(o2.uid),
  16443.         o2.name,
  16444.         c2.name,
  16445.         convert(binary(16),null),
  16446.         convert(int,null),
  16447.         convert(int,8),
  16448.         'NO ACTION',
  16449.         'NO ACTION',
  16450.         i.name,
  16451.         object_name(r.constid),
  16452.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16453.     from
  16454.         sysobjects o1, sysobjects o2,
  16455.         syscolumns c1, syscolumns c2,
  16456.         sysreferences r, sysindexes i, sysusers u
  16457.     where    
  16458.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16459.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16460.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16461.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16462.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16463.     and    o1.id = r.rkeyid
  16464.     and    r.keycnt >= 8
  16465.     and    o1.id = c1.id
  16466.     and    c1.colid = r.rkey8
  16467.     and r.fkeyid = o2.id
  16468.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16469.     and    o2.id = c2.id    
  16470.     and    c2.colid = r.fkey8
  16471.     and i.id = r.rkeyid
  16472.     and i.indid = r.rkeyindid
  16473.     and u.uid = user_id()
  16474.     and ( suser_id() = 1     /* User is the System Administrator */
  16475.     or (
  16476.         o1.uid = user_id()     /* User created the object */
  16477.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16478.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16479.             from sysprotects p
  16480.             where p.id = o1.id
  16481.             /* get rows for public,current user,user's group */
  16482.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16483.             /* check for SELECT,EXECUTE privilege */
  16484.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16485.             ) = 1    /* final magic...compare Grants      */
  16486.         )
  16487.     and (
  16488.         o2.uid = user_id()     /* User created the object */
  16489.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16490.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16491.             from sysprotects p
  16492.             where p.id = o2.id
  16493.             /* get rows for public,current user,user's group */
  16494.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16495.             /* check for SELECT,EXECUTE privilege */
  16496.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16497.             ) = 1    /* final magic...compare Grants      */
  16498.         )
  16499.     )    
  16500.     union all
  16501.     select
  16502.         db_name(r.rkeydbid),
  16503.         user_name(o1.uid),
  16504.         o1.name,
  16505.         c1.name,
  16506.         convert(binary(16),null),
  16507.         convert(int,null),
  16508.         db_name(r.fkeydbid),
  16509.         user_name(o2.uid),
  16510.         o2.name,
  16511.         c2.name,
  16512.         convert(binary(16),null),
  16513.         convert(int,null),
  16514.         convert(int,9),
  16515.         'NO ACTION',
  16516.         'NO ACTION',
  16517.         i.name,
  16518.         object_name(r.constid),
  16519.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16520.     from
  16521.         sysobjects o1, sysobjects o2,
  16522.         syscolumns c1, syscolumns c2,
  16523.         sysreferences r, sysindexes i, sysusers u
  16524.     where    
  16525.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16526.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16527.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16528.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16529.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16530.     and    o1.id = r.rkeyid
  16531.     and    r.keycnt >= 9
  16532.     and    o1.id = c1.id
  16533.     and    c1.colid = r.rkey9
  16534.     and r.fkeyid = o2.id
  16535.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16536.     and    o2.id = c2.id    
  16537.     and    c2.colid = r.fkey9
  16538.     and i.id = r.rkeyid
  16539.     and i.indid = r.rkeyindid
  16540.     and u.uid = user_id()
  16541.     and ( suser_id() = 1     /* User is the System Administrator */
  16542.     or (
  16543.         o1.uid = user_id()     /* User created the object */
  16544.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16545.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16546.             from sysprotects p
  16547.             where p.id = o1.id
  16548.             /* get rows for public,current user,user's group */
  16549.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16550.             /* check for SELECT,EXECUTE privilege */
  16551.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16552.             ) = 1    /* final magic...compare Grants      */
  16553.         )
  16554.     and (
  16555.         o2.uid = user_id()     /* User created the object */
  16556.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16557.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16558.             from sysprotects p
  16559.             where p.id = o2.id
  16560.             /* get rows for public,current user,user's group */
  16561.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16562.             /* check for SELECT,EXECUTE privilege */
  16563.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16564.             ) = 1    /* final magic...compare Grants      */
  16565.         )
  16566.     )    
  16567.     union all
  16568.     select
  16569.         db_name(r.rkeydbid),
  16570.         user_name(o1.uid),
  16571.         o1.name,
  16572.         c1.name,
  16573.         convert(binary(16),null),
  16574.         convert(int,null),
  16575.         db_name(r.fkeydbid),
  16576.         user_name(o2.uid),
  16577.         o2.name,
  16578.         c2.name,
  16579.         convert(binary(16),null),
  16580.         convert(int,null),
  16581.         convert(int,10),
  16582.         'NO ACTION',
  16583.         'NO ACTION',
  16584.         i.name,
  16585.         object_name(r.constid),
  16586.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16587.     from
  16588.         sysobjects o1, sysobjects o2,
  16589.         syscolumns c1, syscolumns c2,
  16590.         sysreferences r, sysindexes i, sysusers u
  16591.     where    
  16592.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16593.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16594.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16595.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16596.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16597.     and    o1.id = r.rkeyid
  16598.     and    r.keycnt >= 10
  16599.     and    o1.id = c1.id
  16600.     and    c1.colid = r.rkey10
  16601.     and r.fkeyid = o2.id
  16602.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16603.     and    o2.id = c2.id    
  16604.     and    c2.colid = r.fkey10
  16605.     and i.id = r.rkeyid
  16606.     and i.indid = r.rkeyindid
  16607.     and u.uid = user_id()
  16608.     and ( suser_id() = 1     /* User is the System Administrator */
  16609.     or (
  16610.         o1.uid = user_id()     /* User created the object */
  16611.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16612.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16613.             from sysprotects p
  16614.             where p.id = o1.id
  16615.             /* get rows for public,current user,user's group */
  16616.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16617.             /* check for SELECT,EXECUTE privilege */
  16618.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16619.             ) = 1    /* final magic...compare Grants      */
  16620.         )
  16621.     and (
  16622.         o2.uid = user_id()     /* User created the object */
  16623.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16624.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16625.             from sysprotects p
  16626.             where p.id = o2.id
  16627.             /* get rows for public,current user,user's group */
  16628.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16629.             /* check for SELECT,EXECUTE privilege */
  16630.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16631.             ) = 1    /* final magic...compare Grants      */
  16632.         )
  16633.     )    
  16634.     union all
  16635.     select
  16636.         db_name(r.rkeydbid),
  16637.         user_name(o1.uid),
  16638.         o1.name,
  16639.         c1.name,
  16640.         convert(binary(16),null),
  16641.         convert(int,null),
  16642.         db_name(r.fkeydbid),
  16643.         user_name(o2.uid),
  16644.         o2.name,
  16645.         c2.name,
  16646.         convert(binary(16),null),
  16647.         convert(int,null),
  16648.         convert(int,11),
  16649.         'NO ACTION',
  16650.         'NO ACTION',
  16651.         i.name,
  16652.         object_name(r.constid),
  16653.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16654.     from
  16655.         sysobjects o1, sysobjects o2,
  16656.         syscolumns c1, syscolumns c2,
  16657.         sysreferences r, sysindexes i, sysusers u
  16658.     where    
  16659.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16660.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16661.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16662.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16663.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16664.     and    o1.id = r.rkeyid
  16665.     and    r.keycnt >= 11
  16666.     and    o1.id = c1.id
  16667.     and    c1.colid = r.rkey11
  16668.     and r.fkeyid = o2.id
  16669.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16670.     and    o2.id = c2.id    
  16671.     and    c2.colid = r.fkey11
  16672.     and i.id = r.rkeyid
  16673.     and i.indid = r.rkeyindid
  16674.     and u.uid = user_id()
  16675.     and ( suser_id() = 1     /* User is the System Administrator */
  16676.     or (
  16677.         o1.uid = user_id()     /* User created the object */
  16678.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16679.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16680.             from sysprotects p
  16681.             where p.id = o1.id
  16682.             /* get rows for public,current user,user's group */
  16683.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16684.             /* check for SELECT,EXECUTE privilege */
  16685.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16686.             ) = 1    /* final magic...compare Grants      */
  16687.         )
  16688.     and (
  16689.         o2.uid = user_id()     /* User created the object */
  16690.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16691.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16692.             from sysprotects p
  16693.             where p.id = o2.id
  16694.             /* get rows for public,current user,user's group */
  16695.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16696.             /* check for SELECT,EXECUTE privilege */
  16697.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16698.             ) = 1    /* final magic...compare Grants      */
  16699.         )
  16700.     )    
  16701.     union all
  16702.     select
  16703.         db_name(r.rkeydbid),
  16704.         user_name(o1.uid),
  16705.         o1.name,
  16706.         c1.name,
  16707.         convert(binary(16),null),
  16708.         convert(int,null),
  16709.         db_name(r.fkeydbid),
  16710.         user_name(o2.uid),
  16711.         o2.name,
  16712.         c2.name,
  16713.         convert(binary(16),null),
  16714.         convert(int,null),
  16715.         convert(int,12),
  16716.         'NO ACTION',
  16717.         'NO ACTION',
  16718.         i.name,
  16719.         object_name(r.constid),
  16720.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16721.     from
  16722.         sysobjects o1, sysobjects o2,
  16723.         syscolumns c1, syscolumns c2,
  16724.         sysreferences r, sysindexes i, sysusers u
  16725.     where    
  16726.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16727.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16728.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16729.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16730.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16731.     and    o1.id = r.rkeyid
  16732.     and    r.keycnt >= 12
  16733.     and    o1.id = c1.id
  16734.     and    c1.colid = r.rkey12
  16735.     and r.fkeyid = o2.id
  16736.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16737.     and    o2.id = c2.id    
  16738.     and    c2.colid = r.fkey12
  16739.     and i.id = r.rkeyid
  16740.     and i.indid = r.rkeyindid
  16741.     and u.uid = user_id()
  16742.     and ( suser_id() = 1     /* User is the System Administrator */
  16743.     or (
  16744.         o1.uid = user_id()     /* User created the object */
  16745.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16746.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16747.             from sysprotects p
  16748.             where p.id = o1.id
  16749.             /* get rows for public,current user,user's group */
  16750.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16751.             /* check for SELECT,EXECUTE privilege */
  16752.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16753.             ) = 1    /* final magic...compare Grants      */
  16754.         )
  16755.     and (
  16756.         o2.uid = user_id()     /* User created the object */
  16757.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16758.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16759.             from sysprotects p
  16760.             where p.id = o2.id
  16761.             /* get rows for public,current user,user's group */
  16762.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16763.             /* check for SELECT,EXECUTE privilege */
  16764.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16765.             ) = 1    /* final magic...compare Grants      */
  16766.         )
  16767.     )    
  16768.     union all
  16769.     select
  16770.         db_name(r.rkeydbid),
  16771.         user_name(o1.uid),
  16772.         o1.name,
  16773.         c1.name,
  16774.         convert(binary(16),null),
  16775.         convert(int,null),
  16776.         db_name(r.fkeydbid),
  16777.         user_name(o2.uid),
  16778.         o2.name,
  16779.         c2.name,
  16780.         convert(binary(16),null),
  16781.         convert(int,null),
  16782.         convert(int,13),
  16783.         'NO ACTION',
  16784.         'NO ACTION',
  16785.         i.name,
  16786.         object_name(r.constid),
  16787.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16788.     from
  16789.         sysobjects o1, sysobjects o2,
  16790.         syscolumns c1, syscolumns c2,
  16791.         sysreferences r, sysindexes i, sysusers u
  16792.     where    
  16793.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16794.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16795.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16796.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16797.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16798.     and    o1.id = r.rkeyid
  16799.     and    r.keycnt >= 13
  16800.     and    o1.id = c1.id
  16801.     and    c1.colid = r.rkey13
  16802.     and r.fkeyid = o2.id
  16803.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16804.     and    o2.id = c2.id    
  16805.     and    c2.colid = r.fkey13
  16806.     and i.id = r.rkeyid
  16807.     and i.indid = r.rkeyindid
  16808.     and u.uid = user_id()
  16809.     and ( suser_id() = 1     /* User is the System Administrator */
  16810.     or (
  16811.         o1.uid = user_id()     /* User created the object */
  16812.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16813.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16814.             from sysprotects p
  16815.             where p.id = o1.id
  16816.             /* get rows for public,current user,user's group */
  16817.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16818.             /* check for SELECT,EXECUTE privilege */
  16819.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16820.             ) = 1    /* final magic...compare Grants      */
  16821.         )
  16822.     and (
  16823.         o2.uid = user_id()     /* User created the object */
  16824.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16825.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16826.             from sysprotects p
  16827.             where p.id = o2.id
  16828.             /* get rows for public,current user,user's group */
  16829.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16830.             /* check for SELECT,EXECUTE privilege */
  16831.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16832.             ) = 1    /* final magic...compare Grants      */
  16833.         )
  16834.     )    
  16835.     union all
  16836.     select
  16837.         db_name(r.rkeydbid),
  16838.         user_name(o1.uid),
  16839.         o1.name,
  16840.         c1.name,
  16841.         convert(binary(16),null),
  16842.         convert(int,null),
  16843.         db_name(r.fkeydbid),
  16844.         user_name(o2.uid),
  16845.         o2.name,
  16846.         c2.name,
  16847.         convert(binary(16),null),
  16848.         convert(int,null),
  16849.         convert(int,14),
  16850.         'NO ACTION',
  16851.         'NO ACTION',
  16852.         i.name,
  16853.         object_name(r.constid),
  16854.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16855.     from
  16856.         sysobjects o1, sysobjects o2,
  16857.         syscolumns c1, syscolumns c2,
  16858.         sysreferences r, sysindexes i, sysusers u
  16859.     where    
  16860.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16861.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16862.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16863.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16864.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16865.     and    o1.id = r.rkeyid
  16866.     and    r.keycnt >= 14
  16867.     and    o1.id = c1.id
  16868.     and    c1.colid = r.rkey14
  16869.     and r.fkeyid = o2.id
  16870.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16871.     and    o2.id = c2.id    
  16872.     and    c2.colid = r.fkey14
  16873.     and i.id = r.rkeyid
  16874.     and i.indid = r.rkeyindid
  16875.     and u.uid = user_id()
  16876.     and ( suser_id() = 1     /* User is the System Administrator */
  16877.     or (
  16878.         o1.uid = user_id()     /* User created the object */
  16879.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16880.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16881.             from sysprotects p
  16882.             where p.id = o1.id
  16883.             /* get rows for public,current user,user's group */
  16884.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16885.             /* check for SELECT,EXECUTE privilege */
  16886.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16887.             ) = 1    /* final magic...compare Grants      */
  16888.         )
  16889.     and (
  16890.         o2.uid = user_id()     /* User created the object */
  16891.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16892.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16893.             from sysprotects p
  16894.             where p.id = o2.id
  16895.             /* get rows for public,current user,user's group */
  16896.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16897.             /* check for SELECT,EXECUTE privilege */
  16898.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16899.             ) = 1    /* final magic...compare Grants      */
  16900.         )
  16901.     )    
  16902.     union all
  16903.     select
  16904.         db_name(r.rkeydbid),
  16905.         user_name(o1.uid),
  16906.         o1.name,
  16907.         c1.name,
  16908.         convert(binary(16),null),
  16909.         convert(int,null),
  16910.         db_name(r.fkeydbid),
  16911.         user_name(o2.uid),
  16912.         o2.name,
  16913.         c2.name,
  16914.         convert(binary(16),null),
  16915.         convert(int,null),
  16916.         convert(int,15),
  16917.         'NO ACTION',
  16918.         'NO ACTION',
  16919.         i.name,
  16920.         object_name(r.constid),
  16921.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16922.     from
  16923.         sysobjects o1, sysobjects o2,
  16924.         syscolumns c1, syscolumns c2,
  16925.         sysreferences r, sysindexes i, sysusers u
  16926.     where    
  16927.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16928.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16929.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16930.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16931.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16932.     and    o1.id = r.rkeyid
  16933.     and    r.keycnt >= 15
  16934.     and    o1.id = c1.id
  16935.     and    c1.colid = r.rkey15
  16936.     and r.fkeyid = o2.id
  16937.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  16938.     and    o2.id = c2.id    
  16939.     and    c2.colid = r.fkey15
  16940.     and i.id = r.rkeyid
  16941.     and i.indid = r.rkeyindid
  16942.     and u.uid = user_id()
  16943.     and ( suser_id() = 1     /* User is the System Administrator */
  16944.     or (
  16945.         o1.uid = user_id()     /* User created the object */
  16946.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16947.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16948.             from sysprotects p
  16949.             where p.id = o1.id
  16950.             /* get rows for public,current user,user's group */
  16951.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16952.             /* check for SELECT,EXECUTE privilege */
  16953.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16954.             ) = 1    /* final magic...compare Grants      */
  16955.         )
  16956.     and (
  16957.         o2.uid = user_id()     /* User created the object */
  16958.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  16959.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  16960.             from sysprotects p
  16961.             where p.id = o2.id
  16962.             /* get rows for public,current user,user's group */
  16963.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  16964.             /* check for SELECT,EXECUTE privilege */
  16965.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  16966.             ) = 1    /* final magic...compare Grants      */
  16967.         )
  16968.     )    
  16969.     union all
  16970.     select
  16971.         db_name(r.rkeydbid),
  16972.         user_name(o1.uid),
  16973.         o1.name,
  16974.         c1.name,
  16975.         convert(binary(16),null),
  16976.         convert(int,null),
  16977.         db_name(r.fkeydbid),
  16978.         user_name(o2.uid),
  16979.         o2.name,
  16980.         c2.name,
  16981.         convert(binary(16),null),
  16982.         convert(int,null),
  16983.         convert(int,16),
  16984.         'NO ACTION',
  16985.         'NO ACTION',
  16986.         i.name,
  16987.         object_name(r.constid),
  16988.         convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  16989.     from
  16990.         sysobjects o1, sysobjects o2,
  16991.         syscolumns c1, syscolumns c2,
  16992.         sysreferences r, sysindexes i, sysusers u
  16993.     where    
  16994.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  16995.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  16996.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  16997.     and (@pk_table_name is null or @pk_table_name = o1.name)
  16998.     and (@fk_table_name is null or @fk_table_name = o2.name)
  16999.     and    o1.id = r.rkeyid
  17000.     and    o1.id = c1.id
  17001.     and    r.keycnt >= 16
  17002.     and    c1.colid = r.rkey16
  17003.     and r.fkeyid = o2.id
  17004.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17005.     and    o2.id = c2.id    
  17006.     and    c2.colid = r.fkey16
  17007.     and i.id = r.rkeyid
  17008.     and i.indid = r.rkeyindid
  17009.     and u.uid = user_id()
  17010.     and ( suser_id() = 1     /* User is the System Administrator */
  17011.     or (
  17012.         o1.uid = user_id()     /* User created the object */
  17013.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17014.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17015.             from sysprotects p
  17016.             where p.id = o1.id
  17017.             /* get rows for public,current user,user's group */
  17018.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17019.             /* check for SELECT,EXECUTE privilege */
  17020.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17021.             ) = 1    /* final magic...compare Grants      */
  17022.         )
  17023.     and (
  17024.         o2.uid = user_id()     /* User created the object */
  17025.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  17026.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  17027.             from sysprotects p
  17028.             where p.id = o2.id
  17029.             /* get rows for public,current user,user's group */
  17030.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  17031.             /* check for SELECT,EXECUTE privilege */
  17032.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  17033.             ) = 1    /* final magic...compare Grants      */
  17034.         )
  17035.     )
  17036.     order by 8,9,2,3,13
  17037.     END
  17038.  
  17039.     exec @ret = sp_cursoropen @handle output, 'select * from #spfkeysrowset1',
  17040.         @scrollopt output, @ccopt output, @rows output
  17041.  
  17042.     drop table #spfkeysrowset1
  17043.        return isnull(@ret,0)
  17044. go
  17045. dump tran master with no_log
  17046. go
  17047. CREATE PROCEDURE sp_foreign_keys_rowset;3
  17048. as
  17049.     select
  17050.         PK_TABLE_CATALOG    = convert(sysname,' '),
  17051.         PK_TABLE_SCHEMA        = convert(sysname,' '),
  17052.         PK_TABLE_NAME         = convert(sysname,' '),
  17053.         PK_COLUMN_NAME         = convert(sysname,' '),
  17054.         PK_COLUMN_GUID        = convert(binary(16),null),
  17055.         PK_COLUMN_PROPID    = convert(int,null),
  17056.         FK_TABLE_CATALOG    = convert(sysname,' '),
  17057.         FK_TABLE_SCHEMA        = convert(sysname,' '),
  17058.         FK_TABLE_NAME         = convert(sysname,' '),
  17059.         FK_COLUMN_NAME         = convert(sysname,' '),
  17060.         FK_COLUMN_GUID        = convert(binary(16),null),
  17061.         FK_COLUMN_PROPID    = convert(int,null),
  17062.         ORDINAL                = convert(int,1),
  17063.         UPDATE_RULE            = 'NO ACTION',
  17064.         DELETE_RULE         = 'NO ACTION',
  17065.         PK_NAME                = convert(sysname, ' '),
  17066.         FK_NAME                = convert(sysname, ' '),
  17067.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17068.     where    1=0
  17069. go
  17070. dump tran master with no_log
  17071. go
  17072.  
  17073.  
  17074. if (charindex('7.00', @@version) = 0 and
  17075.     charindex('8.00', @@version) = 0)
  17076. begin
  17077.     print ''
  17078.     print ''
  17079.     print 'Warning:'
  17080.     print 'you are installing the stored procedures '
  17081.     print 'on a pre 8.0 SQL Server.'
  17082.     print 'Ignore the following errors.'
  17083. end
  17084. else
  17085.     drop proc sp_foreign_keys_rowset
  17086. go
  17087.  
  17088.  
  17089. /*    Procedure for 8.0 server */
  17090. CREATE PROCEDURE sp_foreign_keys_rowset
  17091.     (
  17092.     @pk_table_name        sysname,
  17093.     @pk_table_schema    sysname = null,
  17094.     @fk_table_name        sysname = null,
  17095.     @fk_table_schema    sysname = null,
  17096.     @fk_table_catalog    sysname = null
  17097.     )
  17098. as
  17099.     select
  17100.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17101.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17102.         PK_TABLE_NAME         = o1.name,
  17103.         PK_COLUMN_NAME         = c1.name,
  17104.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17105.         PK_COLUMN_PROPID    = convert(int,null),
  17106.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17107.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17108.         FK_TABLE_NAME         = o2.name,
  17109.         FK_COLUMN_NAME         = c2.name,
  17110.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17111.         FK_COLUMN_PROPID    = convert(int,null),
  17112.         ORDINAL                = convert(int,1),
  17113.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17114.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17115.         PK_NAME                = i.name,
  17116.         FK_NAME                = object_name(r.constid),
  17117.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17118.     from
  17119.         sysobjects o1, sysobjects o2,
  17120.         syscolumns c1, syscolumns c2,
  17121.         sysreferences r, sysindexes i
  17122.     where    
  17123.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17124.     and    o1.name = @pk_table_name
  17125.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17126.     and    o1.id = r.rkeyid
  17127.     and    o1.id = c1.id
  17128.     and    c1.colid = r.rkey1
  17129.     and r.fkeyid = o2.id
  17130.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17131.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17132.     and    o2.id = c2.id    
  17133.     and    c2.colid = r.fkey1
  17134.     and permissions(o1.id) <> 0
  17135.     and permissions(o2.id) <> 0
  17136.     and i.id = r.rkeyid
  17137.     and i.indid = r.rkeyindid
  17138.     union all
  17139.     select
  17140.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17141.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17142.         PK_TABLE_NAME         = o1.name,
  17143.         PK_COLUMN_NAME         = c1.name,
  17144.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17145.         PK_COLUMN_PROPID    = convert(int,null),
  17146.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17147.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17148.         FK_TABLE_NAME         = o2.name,
  17149.         FK_COLUMN_NAME         = c2.name,
  17150.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17151.         FK_COLUMN_PROPID    = convert(int,null),
  17152.         ORDINAL                = convert(int,2),
  17153.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17154.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17155.         PK_NAME                = i.name,
  17156.         FK_NAME                = object_name(r.constid),
  17157.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17158.     from
  17159.         sysobjects o1, sysobjects o2,
  17160.         syscolumns c1, syscolumns c2,
  17161.         sysreferences r, sysindexes i
  17162.     where    
  17163.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17164.     and    o1.name = @pk_table_name
  17165.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17166.     and    o1.id = r.rkeyid
  17167.     and    o1.id = c1.id
  17168.     and    c1.colid = r.rkey2
  17169.     and    r.keycnt >= 2
  17170.     and r.fkeyid = o2.id
  17171.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17172.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17173.     and    o2.id = c2.id    
  17174.     and    c2.colid = r.fkey2
  17175.     and permissions(o1.id) <> 0
  17176.     and permissions(o2.id) <> 0
  17177.     and i.id = r.rkeyid
  17178.     and i.indid = r.rkeyindid
  17179.     union all
  17180.     select
  17181.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17182.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17183.         PK_TABLE_NAME         = o1.name,
  17184.         PK_COLUMN_NAME         = c1.name,
  17185.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17186.         PK_COLUMN_PROPID    = convert(int,null),
  17187.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17188.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17189.         FK_TABLE_NAME         = o2.name,
  17190.         FK_COLUMN_NAME         = c2.name,
  17191.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17192.         FK_COLUMN_PROPID    = convert(int,null),
  17193.         ORDINAL                = convert(int,3),
  17194.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17195.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17196.         PK_NAME                = i.name,
  17197.         FK_NAME                = object_name(r.constid),
  17198.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17199.     from
  17200.         sysobjects o1, sysobjects o2,
  17201.         syscolumns c1, syscolumns c2,
  17202.         sysreferences r, sysindexes i
  17203.     where    
  17204.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17205.     and    o1.name = @pk_table_name
  17206.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17207.     and    o1.id = r.rkeyid
  17208.     and    o1.id = c1.id
  17209.     and    c1.colid = r.rkey3
  17210.     and    r.keycnt >= 3
  17211.     and r.fkeyid = o2.id
  17212.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17213.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17214.     and    o2.id = c2.id    
  17215.     and    c2.colid = r.fkey3
  17216.     and permissions(o1.id) <> 0
  17217.     and permissions(o2.id) <> 0    
  17218.     and i.id = r.rkeyid
  17219.     and i.indid = r.rkeyindid
  17220.     union all
  17221.     select
  17222.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17223.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17224.         PK_TABLE_NAME         = o1.name,
  17225.         PK_COLUMN_NAME         = c1.name,
  17226.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17227.         PK_COLUMN_PROPID    = convert(int,null),
  17228.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17229.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17230.         FK_TABLE_NAME         = o2.name,
  17231.         FK_COLUMN_NAME         = c2.name,
  17232.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17233.         FK_COLUMN_PROPID    = convert(int,null),
  17234.         ORDINAL                = convert(int,4),
  17235.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17236.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17237.         PK_NAME                = i.name,
  17238.         FK_NAME                = object_name(r.constid),
  17239.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17240.     from
  17241.         sysobjects o1, sysobjects o2,
  17242.         syscolumns c1, syscolumns c2,
  17243.         sysreferences r, sysindexes i
  17244.     where    
  17245.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17246.     and    o1.name = @pk_table_name
  17247.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17248.     and    o1.id = r.rkeyid
  17249.     and    r.keycnt >= 4
  17250.     and    o1.id = c1.id
  17251.     and    c1.colid = r.rkey4
  17252.     and r.fkeyid = o2.id
  17253.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17254.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17255.     and    o2.id = c2.id    
  17256.     and    c2.colid = r.fkey4
  17257.     and permissions(o1.id) <> 0
  17258.     and permissions(o2.id) <> 0
  17259.     and i.id = r.rkeyid
  17260.     and i.indid = r.rkeyindid    
  17261.     union all
  17262.     select
  17263.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17264.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17265.         PK_TABLE_NAME         = o1.name,
  17266.         PK_COLUMN_NAME         = c1.name,
  17267.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17268.         PK_COLUMN_PROPID    = convert(int,null),
  17269.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17270.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17271.         FK_TABLE_NAME         = o2.name,
  17272.         FK_COLUMN_NAME         = c2.name,
  17273.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17274.         FK_COLUMN_PROPID    = convert(int,null),
  17275.         ORDINAL                = convert(int,5),
  17276.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17277.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17278.         PK_NAME                = i.name,
  17279.         FK_NAME                = object_name(r.constid),
  17280.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17281.     from
  17282.         sysobjects o1, sysobjects o2,
  17283.         syscolumns c1, syscolumns c2,
  17284.         sysreferences r, sysindexes i
  17285.     where    
  17286.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17287.     and    o1.name = @pk_table_name
  17288.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17289.     and    o1.id = r.rkeyid
  17290.     and    r.keycnt >= 5
  17291.     and    o1.id = c1.id
  17292.     and    c1.colid = r.rkey5
  17293.     and r.fkeyid = o2.id
  17294.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17295.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17296.     and    o2.id = c2.id    
  17297.     and    c2.colid = r.fkey5
  17298.     and permissions(o1.id) <> 0
  17299.     and permissions(o2.id) <> 0
  17300.     and i.id = r.rkeyid
  17301.     and i.indid = r.rkeyindid        
  17302.     union all
  17303.     select
  17304.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17305.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17306.         PK_TABLE_NAME         = o1.name,
  17307.         PK_COLUMN_NAME         = c1.name,
  17308.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17309.         PK_COLUMN_PROPID    = convert(int,null),
  17310.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17311.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17312.         FK_TABLE_NAME         = o2.name,
  17313.         FK_COLUMN_NAME         = c2.name,
  17314.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17315.         FK_COLUMN_PROPID    = convert(int,null),
  17316.         ORDINAL                = convert(int,6),
  17317.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17318.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17319.         PK_NAME                = i.name,
  17320.         FK_NAME                = object_name(r.constid),
  17321.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17322.     from
  17323.         sysobjects o1, sysobjects o2,
  17324.         syscolumns c1, syscolumns c2,
  17325.         sysreferences r, sysindexes i
  17326.     where    
  17327.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17328.     and    o1.name = @pk_table_name
  17329.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17330.     and    o1.id = r.rkeyid
  17331.     and    r.keycnt >= 6
  17332.     and    o1.id = c1.id
  17333.     and    c1.colid = r.rkey6
  17334.     and r.fkeyid = o2.id
  17335.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17336.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17337.     and    o2.id = c2.id    
  17338.     and    c2.colid = r.fkey6
  17339.     and permissions(o1.id) <> 0
  17340.     and permissions(o2.id) <> 0
  17341.     and i.id = r.rkeyid
  17342.     and i.indid = r.rkeyindid            
  17343.     union all
  17344.     select
  17345.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17346.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17347.         PK_TABLE_NAME         = o1.name,
  17348.         PK_COLUMN_NAME         = c1.name,
  17349.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17350.         PK_COLUMN_PROPID    = convert(int,null),
  17351.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17352.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17353.         FK_TABLE_NAME         = o2.name,
  17354.         FK_COLUMN_NAME         = c2.name,
  17355.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17356.         FK_COLUMN_PROPID    = convert(int,null),
  17357.         ORDINAL                = convert(int,7),
  17358.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17359.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17360.         PK_NAME                = i.name,
  17361.         FK_NAME                = object_name(r.constid),
  17362.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17363.     from
  17364.         sysobjects o1, sysobjects o2,
  17365.         syscolumns c1, syscolumns c2,
  17366.         sysreferences r, sysindexes i
  17367.     where    
  17368.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17369.     and    o1.name = @pk_table_name
  17370.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17371.     and    o1.id = r.rkeyid
  17372.     and    r.keycnt >= 7
  17373.     and    o1.id = c1.id
  17374.     and    c1.colid = r.rkey7
  17375.     and r.fkeyid = o2.id
  17376.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17377.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17378.     and    o2.id = c2.id    
  17379.     and    c2.colid = r.fkey7
  17380.     and permissions(o1.id) <> 0
  17381.     and permissions(o2.id) <> 0
  17382.     and i.id = r.rkeyid
  17383.     and i.indid = r.rkeyindid            
  17384.     union all
  17385.     select
  17386.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17387.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17388.         PK_TABLE_NAME         = o1.name,
  17389.         PK_COLUMN_NAME         = c1.name,
  17390.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17391.         PK_COLUMN_PROPID    = convert(int,null),
  17392.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17393.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17394.         FK_TABLE_NAME         = o2.name,
  17395.         FK_COLUMN_NAME         = c2.name,
  17396.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17397.         FK_COLUMN_PROPID    = convert(int,null),
  17398.         ORDINAL                = convert(int,8),
  17399.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17400.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17401.         PK_NAME                = i.name,
  17402.         FK_NAME                = object_name(r.constid),
  17403.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17404.     from
  17405.         sysobjects o1, sysobjects o2,
  17406.         syscolumns c1, syscolumns c2,
  17407.         sysreferences r, sysindexes i
  17408.     where    
  17409.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17410.     and    o1.name = @pk_table_name
  17411.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17412.     and    o1.id = r.rkeyid
  17413.     and    r.keycnt >= 8
  17414.     and    o1.id = c1.id
  17415.     and    c1.colid = r.rkey8
  17416.     and r.fkeyid = o2.id
  17417.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17418.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17419.     and    o2.id = c2.id    
  17420.     and    c2.colid = r.fkey8
  17421.     and permissions(o1.id) <> 0
  17422.     and permissions(o2.id) <> 0
  17423.     and i.id = r.rkeyid
  17424.     and i.indid = r.rkeyindid            
  17425.     union all
  17426.     select
  17427.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17428.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17429.         PK_TABLE_NAME         = o1.name,
  17430.         PK_COLUMN_NAME         = c1.name,
  17431.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17432.         PK_COLUMN_PROPID    = convert(int,null),
  17433.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17434.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17435.         FK_TABLE_NAME         = o2.name,
  17436.         FK_COLUMN_NAME         = c2.name,
  17437.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17438.         FK_COLUMN_PROPID    = convert(int,null),
  17439.         ORDINAL                = convert(int,9),
  17440.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17441.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17442.         PK_NAME                = i.name,
  17443.         FK_NAME                = object_name(r.constid),
  17444.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17445.     from
  17446.         sysobjects o1, sysobjects o2,
  17447.         syscolumns c1, syscolumns c2,
  17448.         sysreferences r, sysindexes i
  17449.     where    
  17450.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17451.     and    o1.name = @pk_table_name
  17452.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17453.     and    o1.id = r.rkeyid
  17454.     and    r.keycnt >= 9
  17455.     and    o1.id = c1.id
  17456.     and    c1.colid = r.rkey9
  17457.     and r.fkeyid = o2.id
  17458.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17459.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17460.     and    o2.id = c2.id    
  17461.     and    c2.colid = r.fkey9
  17462.     and permissions(o1.id) <> 0
  17463.     and permissions(o2.id) <> 0
  17464.     and i.id = r.rkeyid
  17465.     and i.indid = r.rkeyindid            
  17466.     union all
  17467.     select
  17468.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17469.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17470.         PK_TABLE_NAME         = o1.name,
  17471.         PK_COLUMN_NAME         = c1.name,
  17472.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17473.         PK_COLUMN_PROPID    = convert(int,null),
  17474.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17475.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17476.         FK_TABLE_NAME         = o2.name,
  17477.         FK_COLUMN_NAME         = c2.name,
  17478.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17479.         FK_COLUMN_PROPID    = convert(int,null),
  17480.         ORDINAL                = convert(int,10),
  17481.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17482.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17483.         PK_NAME                = i.name,
  17484.         FK_NAME                = object_name(r.constid),
  17485.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17486.     from
  17487.         sysobjects o1, sysobjects o2,
  17488.         syscolumns c1, syscolumns c2,
  17489.         sysreferences r, sysindexes i
  17490.     where    
  17491.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17492.     and    o1.name = @pk_table_name
  17493.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17494.     and    o1.id = r.rkeyid
  17495.     and    r.keycnt >= 10
  17496.     and    o1.id = c1.id
  17497.     and    c1.colid = r.rkey10
  17498.     and r.fkeyid = o2.id
  17499.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17500.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17501.     and    o2.id = c2.id    
  17502.     and    c2.colid = r.fkey10
  17503.     and permissions(o1.id) <> 0
  17504.     and permissions(o2.id) <> 0
  17505.     and i.id = r.rkeyid
  17506.     and i.indid = r.rkeyindid            
  17507.     union all
  17508.     select
  17509.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17510.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17511.         PK_TABLE_NAME         = o1.name,
  17512.         PK_COLUMN_NAME         = c1.name,
  17513.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17514.         PK_COLUMN_PROPID    = convert(int,null),
  17515.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17516.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17517.         FK_TABLE_NAME         = o2.name,
  17518.         FK_COLUMN_NAME         = c2.name,
  17519.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17520.         FK_COLUMN_PROPID    = convert(int,null),
  17521.         ORDINAL                = convert(int,11),
  17522.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17523.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17524.         PK_NAME                = i.name,
  17525.         FK_NAME                = object_name(r.constid),
  17526.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17527.     from
  17528.         sysobjects o1, sysobjects o2,
  17529.         syscolumns c1, syscolumns c2,
  17530.         sysreferences r, sysindexes i
  17531.     where    
  17532.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17533.     and    o1.name = @pk_table_name
  17534.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17535.     and    o1.id = r.rkeyid
  17536.     and    r.keycnt >= 11
  17537.     and    o1.id = c1.id
  17538.     and    c1.colid = r.rkey11
  17539.     and r.fkeyid = o2.id
  17540.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17541.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17542.     and    o2.id = c2.id    
  17543.     and    c2.colid = r.fkey11
  17544.     and permissions(o1.id) <> 0
  17545.     and permissions(o2.id) <> 0
  17546.     and i.id = r.rkeyid
  17547.     and i.indid = r.rkeyindid            
  17548.     union all
  17549.     select
  17550.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17551.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17552.         PK_TABLE_NAME         = o1.name,
  17553.         PK_COLUMN_NAME         = c1.name,
  17554.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17555.         PK_COLUMN_PROPID    = convert(int,null),
  17556.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17557.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17558.         FK_TABLE_NAME         = o2.name,
  17559.         FK_COLUMN_NAME         = c2.name,
  17560.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17561.         FK_COLUMN_PROPID    = convert(int,null),
  17562.         ORDINAL                = convert(int,12),
  17563.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17564.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17565.         PK_NAME                = i.name,
  17566.         FK_NAME                = object_name(r.constid),
  17567.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17568.     from
  17569.         sysobjects o1, sysobjects o2,
  17570.         syscolumns c1, syscolumns c2,
  17571.         sysreferences r, sysindexes i
  17572.     where    
  17573.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17574.     and    o1.name = @pk_table_name
  17575.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17576.     and    o1.id = r.rkeyid
  17577.     and    r.keycnt >= 12
  17578.     and    o1.id = c1.id
  17579.     and    c1.colid = r.rkey12
  17580.     and r.fkeyid = o2.id
  17581.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17582.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17583.     and    o2.id = c2.id    
  17584.     and    c2.colid = r.fkey12
  17585.     and permissions(o1.id) <> 0
  17586.     and permissions(o2.id) <> 0
  17587.     and i.id = r.rkeyid
  17588.     and i.indid = r.rkeyindid            
  17589.     union all
  17590.     select
  17591.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17592.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17593.         PK_TABLE_NAME         = o1.name,
  17594.         PK_COLUMN_NAME         = c1.name,
  17595.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17596.         PK_COLUMN_PROPID    = convert(int,null),
  17597.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17598.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17599.         FK_TABLE_NAME         = o2.name,
  17600.         FK_COLUMN_NAME         = c2.name,
  17601.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17602.         FK_COLUMN_PROPID    = convert(int,null),
  17603.         ORDINAL                = convert(int,13),
  17604.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17605.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17606.         PK_NAME                = i.name,
  17607.         FK_NAME                = object_name(r.constid),
  17608.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17609.     from
  17610.         sysobjects o1, sysobjects o2,
  17611.         syscolumns c1, syscolumns c2,
  17612.         sysreferences r, sysindexes i
  17613.     where    
  17614.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17615.     and    o1.name = @pk_table_name
  17616.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17617.     and    o1.id = r.rkeyid
  17618.     and    r.keycnt >= 13
  17619.     and    o1.id = c1.id
  17620.     and    c1.colid = r.rkey13
  17621.     and r.fkeyid = o2.id
  17622.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17623.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17624.     and    o2.id = c2.id    
  17625.     and    c2.colid = r.fkey13
  17626.     and permissions(o1.id) <> 0
  17627.     and permissions(o2.id) <> 0
  17628.     and i.id = r.rkeyid
  17629.     and i.indid = r.rkeyindid            
  17630.     union all
  17631.     select
  17632.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17633.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17634.         PK_TABLE_NAME         = o1.name,
  17635.         PK_COLUMN_NAME         = c1.name,
  17636.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17637.         PK_COLUMN_PROPID    = convert(int,null),
  17638.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17639.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17640.         FK_TABLE_NAME         = o2.name,
  17641.         FK_COLUMN_NAME         = c2.name,
  17642.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17643.         FK_COLUMN_PROPID    = convert(int,null),
  17644.         ORDINAL                = convert(int,14),
  17645.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17646.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17647.         PK_NAME                = i.name,
  17648.         FK_NAME                = object_name(r.constid),
  17649.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17650.     from
  17651.         sysobjects o1, sysobjects o2,
  17652.         syscolumns c1, syscolumns c2,
  17653.         sysreferences r, sysindexes i
  17654.     where    
  17655.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17656.     and    o1.name = @pk_table_name
  17657.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17658.     and    o1.id = r.rkeyid
  17659.     and    r.keycnt >= 14
  17660.     and    o1.id = c1.id
  17661.     and    c1.colid = r.rkey14
  17662.     and r.fkeyid = o2.id
  17663.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17664.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17665.     and    o2.id = c2.id    
  17666.     and    c2.colid = r.fkey14
  17667.     and permissions(o1.id) <> 0
  17668.     and permissions(o2.id) <> 0
  17669.     and i.id = r.rkeyid
  17670.     and i.indid = r.rkeyindid            
  17671.     union all
  17672.     select
  17673.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17674.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17675.         PK_TABLE_NAME         = o1.name,
  17676.         PK_COLUMN_NAME         = c1.name,
  17677.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17678.         PK_COLUMN_PROPID    = convert(int,null),
  17679.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17680.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17681.         FK_TABLE_NAME         = o2.name,
  17682.         FK_COLUMN_NAME         = c2.name,
  17683.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17684.         FK_COLUMN_PROPID    = convert(int,null),
  17685.         ORDINAL                = convert(int,15),
  17686.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17687.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17688.         PK_NAME                = i.name,
  17689.         FK_NAME                = object_name(r.constid),
  17690.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17691.     from
  17692.         sysobjects o1, sysobjects o2,
  17693.         syscolumns c1, syscolumns c2,
  17694.         sysreferences r, sysindexes i
  17695.     where    
  17696.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17697.     and    o1.name = @pk_table_name
  17698.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17699.     and    o1.id = r.rkeyid
  17700.     and    r.keycnt >= 15
  17701.     and    o1.id = c1.id
  17702.     and    c1.colid = r.rkey15
  17703.     and r.fkeyid = o2.id
  17704.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17705.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17706.     and    o2.id = c2.id    
  17707.     and    c2.colid = r.fkey15
  17708.     and permissions(o1.id) <> 0
  17709.     and permissions(o2.id) <> 0
  17710.     and i.id = r.rkeyid
  17711.     and i.indid = r.rkeyindid            
  17712.     union all
  17713.     select
  17714.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17715.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17716.         PK_TABLE_NAME         = o1.name,
  17717.         PK_COLUMN_NAME         = c1.name,
  17718.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17719.         PK_COLUMN_PROPID    = convert(int,null),
  17720.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17721.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17722.         FK_TABLE_NAME         = o2.name,
  17723.         FK_COLUMN_NAME         = c2.name,
  17724.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17725.         FK_COLUMN_PROPID    = convert(int,null),
  17726.         ORDINAL                = convert(int,16),
  17727.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17728.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17729.         PK_NAME                = i.name,
  17730.         FK_NAME                = object_name(r.constid),
  17731.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17732.     from
  17733.         sysobjects o1, sysobjects o2,
  17734.         syscolumns c1, syscolumns c2,
  17735.         sysreferences r, sysindexes i
  17736.     where    
  17737.         (@fk_table_catalog is null or @fk_table_catalog = db_name())
  17738.     and    o1.name = @pk_table_name
  17739.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17740.     and    o1.id = r.rkeyid
  17741.     and    r.keycnt >= 16
  17742.     and    o1.id = c1.id
  17743.     and    c1.colid = r.rkey16
  17744.     and r.fkeyid = o2.id
  17745.     and    (@fk_table_name is null or @fk_table_name = o2.name)
  17746.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17747.     and    o2.id = c2.id    
  17748.     and    c2.colid = r.fkey16
  17749.     and permissions(o1.id) <> 0
  17750.     and permissions(o2.id) <> 0
  17751.     and i.id = r.rkeyid
  17752.     and i.indid = r.rkeyindid        
  17753.     order by 8,9,2,3,13
  17754. go
  17755. dump tran master with no_log
  17756. go
  17757. CREATE PROCEDURE sp_foreign_keys_rowset;2
  17758.     (
  17759.     @fk_table_name        sysname,
  17760.     @fk_table_schema    sysname = null,
  17761.     @pk_table_name        sysname = null,
  17762.     @pk_table_schema    sysname = null,
  17763.     @pk_table_catalog    sysname = null
  17764.     )
  17765. as
  17766.     select
  17767.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17768.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17769.         PK_TABLE_NAME         = o1.name,
  17770.         PK_COLUMN_NAME         = c1.name,
  17771.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17772.         PK_COLUMN_PROPID    = convert(int,null),
  17773.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17774.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17775.         FK_TABLE_NAME         = o2.name,
  17776.         FK_COLUMN_NAME         = c2.name,
  17777.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17778.         FK_COLUMN_PROPID    = convert(int,null),
  17779.         ORDINAL                = convert(int,1),
  17780.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17781.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17782.         PK_NAME                = i.name,
  17783.         FK_NAME                = object_name(r.constid),
  17784.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17785.     from
  17786.         sysobjects o1, sysobjects o2,
  17787.         syscolumns c1, syscolumns c2,
  17788.         sysreferences r, sysindexes i
  17789.     where    
  17790.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17791.     and    o2.name = @fk_table_name
  17792.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17793.     and    o2.id = r.fkeyid
  17794.     and    o2.id = c2.id
  17795.     and    c2.colid = r.fkey1
  17796.     and r.rkeyid = o1.id
  17797.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17798.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17799.     and    o1.id = c1.id    
  17800.     and    c1.colid = r.rkey1
  17801.     and permissions(o1.id) <> 0
  17802.     and permissions(o2.id) <> 0
  17803.     and i.id = r.rkeyid
  17804.     and i.indid = r.rkeyindid        
  17805.     union    all
  17806.     select
  17807.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17808.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17809.         PK_TABLE_NAME         = o1.name,
  17810.         PK_COLUMN_NAME         = c1.name,
  17811.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17812.         PK_COLUMN_PROPID    = convert(int,null),
  17813.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17814.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17815.         FK_TABLE_NAME         = o2.name,
  17816.         FK_COLUMN_NAME         = c2.name,
  17817.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17818.         FK_COLUMN_PROPID    = convert(int,null),
  17819.         ORDINAL                = convert(int,2),
  17820.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17821.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17822.         PK_NAME                = i.name,
  17823.         FK_NAME                = object_name(r.constid),
  17824.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17825.     from
  17826.         sysobjects o1, sysobjects o2,
  17827.         syscolumns c1, syscolumns c2,
  17828.         sysreferences r, sysindexes i
  17829.     where    
  17830.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17831.     and    o2.name = @fk_table_name
  17832.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17833.     and    o2.id = r.fkeyid
  17834.     and    r.keycnt >= 2
  17835.     and    o2.id = c2.id
  17836.     and    c2.colid = r.fkey2
  17837.     and    r.rkeyid = o1.id
  17838.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17839.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17840.     and    o1.id = c1.id    
  17841.     and    c1.colid = r.rkey2
  17842.     and permissions(o1.id) <> 0
  17843.     and permissions(o2.id) <> 0
  17844.     and i.id = r.rkeyid
  17845.     and i.indid = r.rkeyindid            
  17846.     union    all
  17847.     select
  17848.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17849.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17850.         PK_TABLE_NAME         = o1.name,
  17851.         PK_COLUMN_NAME         = c1.name,
  17852.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17853.         PK_COLUMN_PROPID    = convert(int,null),
  17854.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17855.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17856.         FK_TABLE_NAME         = o2.name,
  17857.         FK_COLUMN_NAME         = c2.name,
  17858.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17859.         FK_COLUMN_PROPID    = convert(int,null),
  17860.         ORDINAL                = convert(int,3),
  17861.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17862.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17863.         PK_NAME                = i.name,
  17864.         FK_NAME                = object_name(r.constid),
  17865.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17866.     from
  17867.         sysobjects o1, sysobjects o2,
  17868.         syscolumns c1, syscolumns c2,
  17869.         sysreferences r, sysindexes i
  17870.     where    
  17871.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17872.     and    o2.name = @fk_table_name
  17873.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17874.     and    o2.id = r.fkeyid
  17875.     and    r.keycnt >= 3
  17876.     and    o2.id = c2.id
  17877.     and    c2.colid = r.fkey3
  17878.     and r.rkeyid = o1.id
  17879.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17880.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17881.     and    o1.id = c1.id    
  17882.     and    c1.colid = r.rkey3
  17883.     and permissions(o1.id) <> 0
  17884.     and permissions(o2.id) <> 0
  17885.     and i.id = r.rkeyid
  17886.     and i.indid = r.rkeyindid            
  17887.     union    all
  17888.     select
  17889.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17890.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17891.         PK_TABLE_NAME         = o1.name,
  17892.         PK_COLUMN_NAME         = c1.name,
  17893.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17894.         PK_COLUMN_PROPID    = convert(int,null),
  17895.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17896.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  17897.         FK_TABLE_NAME         = o2.name,
  17898.         FK_COLUMN_NAME         = c2.name,
  17899.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17900.         FK_COLUMN_PROPID    = convert(int,null),
  17901.         ORDINAL                = convert(int,4),
  17902.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17903.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17904.         PK_NAME                = i.name,
  17905.         FK_NAME                = object_name(r.constid),
  17906.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17907.     from
  17908.         sysobjects o1, sysobjects o2,
  17909.         syscolumns c1, syscolumns c2,
  17910.         sysreferences r, sysindexes i
  17911.     where    
  17912.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17913.     and    o2.name = @fk_table_name
  17914.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17915.     and    o2.id = r.fkeyid
  17916.     and    r.keycnt >= 4
  17917.     and    o2.id = c2.id
  17918.     and    c2.colid = r.fkey4
  17919.     and r.rkeyid = o1.id
  17920.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17921.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17922.     and    o1.id = c1.id    
  17923.     and    c1.colid = r.rkey4
  17924.     and permissions(o1.id) <> 0
  17925.     and permissions(o2.id) <> 0
  17926.     and i.id = r.rkeyid
  17927.     and i.indid = r.rkeyindid            
  17928.     union    all
  17929.     select
  17930.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17931.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17932.         PK_TABLE_NAME         = o1.name,
  17933.         PK_COLUMN_NAME         = c1.name,
  17934.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17935.         PK_COLUMN_PROPID    = convert(int,null),
  17936.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17937.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17938.         FK_TABLE_NAME         = o2.name,
  17939.         FK_COLUMN_NAME         = c2.name,
  17940.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17941.         FK_COLUMN_PROPID    = convert(int,null),
  17942.         ORDINAL                = convert(int,5),
  17943.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17944.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17945.         PK_NAME                = i.name,
  17946.         FK_NAME                = object_name(r.constid),
  17947.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17948.     from
  17949.         sysobjects o1, sysobjects o2,
  17950.         syscolumns c1, syscolumns c2,
  17951.         sysreferences r, sysindexes i
  17952.     where    
  17953.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17954.     and    o2.name = @fk_table_name
  17955.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17956.     and    o2.id = r.fkeyid
  17957.     and    r.keycnt >= 5
  17958.     and    o2.id = c2.id
  17959.     and    c2.colid = r.fkey5
  17960.     and    r.rkeyid = o1.id
  17961.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  17962.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  17963.     and    o1.id = c1.id    
  17964.     and    c1.colid = r.rkey5
  17965.     and permissions(o1.id) <> 0
  17966.     and permissions(o2.id) <> 0
  17967.     and i.id = r.rkeyid
  17968.     and i.indid = r.rkeyindid            
  17969.     union    all
  17970.     select
  17971.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  17972.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  17973.         PK_TABLE_NAME         = o1.name,
  17974.         PK_COLUMN_NAME         = c1.name,
  17975.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17976.         PK_COLUMN_PROPID    = convert(int,null),
  17977.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  17978.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  17979.         FK_TABLE_NAME         = o2.name,
  17980.         FK_COLUMN_NAME         = c2.name,
  17981.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  17982.         FK_COLUMN_PROPID    = convert(int,null),
  17983.         ORDINAL                = convert(int,6),
  17984.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17985.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  17986.         PK_NAME                = i.name,
  17987.         FK_NAME                = object_name(r.constid),
  17988.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  17989.     from
  17990.         sysobjects o1, sysobjects o2,
  17991.         syscolumns c1, syscolumns c2,
  17992.         sysreferences r, sysindexes i
  17993.     where    
  17994.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  17995.     and    o2.name = @fk_table_name
  17996.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  17997.     and    o2.id = r.fkeyid
  17998.     and    r.keycnt >= 6
  17999.     and    o2.id = c2.id
  18000.     and    c2.colid = r.fkey6
  18001.     and r.rkeyid = o1.id
  18002.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18003.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18004.     and    o1.id = c1.id        
  18005.     and    c1.colid = r.rkey6
  18006.     and permissions(o1.id) <> 0
  18007.     and permissions(o2.id) <> 0
  18008.     and i.id = r.rkeyid
  18009.     and i.indid = r.rkeyindid        
  18010.     union    all
  18011.     select
  18012.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18013.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18014.         PK_TABLE_NAME         = o1.name,
  18015.         PK_COLUMN_NAME         = c1.name,
  18016.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18017.         PK_COLUMN_PROPID    = convert(int,null),
  18018.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18019.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18020.         FK_TABLE_NAME         = o2.name,
  18021.         FK_COLUMN_NAME         = c2.name,
  18022.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18023.         FK_COLUMN_PROPID    = convert(int,null),
  18024.         ORDINAL                = convert(int,7),
  18025.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18026.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18027.         PK_NAME                = i.name,
  18028.         FK_NAME                = object_name(r.constid),
  18029.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18030.     from
  18031.         sysobjects o1, sysobjects o2,
  18032.         syscolumns c1, syscolumns c2,
  18033.         sysreferences r, sysindexes i
  18034.     where    
  18035.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18036.     and    o2.name = @fk_table_name
  18037.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18038.     and    o2.id = r.fkeyid
  18039.     and    r.keycnt >= 7
  18040.     and    o2.id = c2.id
  18041.     and    c2.colid = r.fkey7
  18042.     and    r.rkeyid = o1.id
  18043.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18044.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18045.     and    o1.id = c1.id    
  18046.     and    c1.colid = r.rkey7
  18047.     and permissions(o1.id) <> 0
  18048.     and permissions(o2.id) <> 0
  18049.     and i.id = r.rkeyid
  18050.     and i.indid = r.rkeyindid            
  18051.     union    all
  18052.     select
  18053.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18054.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18055.         PK_TABLE_NAME         = o1.name,
  18056.         PK_COLUMN_NAME         = c1.name,
  18057.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18058.         PK_COLUMN_PROPID    = convert(int,null),
  18059.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18060.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18061.         FK_TABLE_NAME         = o2.name,
  18062.         FK_COLUMN_NAME         = c2.name,
  18063.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18064.         FK_COLUMN_PROPID    = convert(int,null),
  18065.         ORDINAL                = convert(int,8),
  18066.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18067.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18068.         PK_NAME                = i.name,
  18069.         FK_NAME                = object_name(r.constid),
  18070.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18071.     from
  18072.         sysobjects o1, sysobjects o2,
  18073.         syscolumns c1, syscolumns c2,
  18074.         sysreferences r, sysindexes i
  18075.     where    
  18076.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18077.     and    o2.name = @fk_table_name
  18078.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18079.     and    o2.id = r.fkeyid
  18080.     and    r.keycnt >= 8
  18081.     and    o2.id = c2.id
  18082.     and    c2.colid = r.fkey8
  18083.     and r.rkeyid = o1.id
  18084.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18085.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18086.     and    o1.id = c1.id    
  18087.     and    c1.colid = r.rkey8
  18088.     and permissions(o1.id) <> 0
  18089.     and permissions(o2.id) <> 0
  18090.     and i.id = r.rkeyid
  18091.     and i.indid = r.rkeyindid            
  18092.     union    all
  18093.     select
  18094.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18095.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18096.         PK_TABLE_NAME         = o1.name,
  18097.         PK_COLUMN_NAME         = c1.name,
  18098.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18099.         PK_COLUMN_PROPID    = convert(int,null),
  18100.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18101.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18102.         FK_TABLE_NAME         = o2.name,
  18103.         FK_COLUMN_NAME         = c2.name,
  18104.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18105.         FK_COLUMN_PROPID    = convert(int,null),
  18106.         ORDINAL                = convert(int,9),
  18107.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18108.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18109.         PK_NAME                = i.name,
  18110.         FK_NAME                = object_name(r.constid),
  18111.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18112.     from
  18113.         sysobjects o1, sysobjects o2,
  18114.         syscolumns c1, syscolumns c2,
  18115.         sysreferences r, sysindexes i
  18116.     where    
  18117.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18118.     and    o2.name = @fk_table_name
  18119.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18120.     and    o2.id = r.fkeyid
  18121.     and    r.keycnt >= 9
  18122.     and    o2.id = c2.id
  18123.     and    c2.colid = r.fkey9
  18124.     and    r.rkeyid = o1.id
  18125.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18126.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18127.     and    o1.id = c1.id    
  18128.     and    c1.colid = r.rkey9
  18129.     and permissions(o1.id) <> 0
  18130.     and permissions(o2.id) <> 0
  18131.     and i.id = r.rkeyid
  18132.     and i.indid = r.rkeyindid            
  18133.     union    all
  18134.     select
  18135.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18136.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18137.         PK_TABLE_NAME         = o1.name,
  18138.         PK_COLUMN_NAME         = c1.name,
  18139.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18140.         PK_COLUMN_PROPID    = convert(int,null),
  18141.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18142.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18143.         FK_TABLE_NAME         = o2.name,
  18144.         FK_COLUMN_NAME         = c2.name,
  18145.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18146.         FK_COLUMN_PROPID    = convert(int,null),
  18147.         ORDINAL                = convert(int,10),
  18148.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18149.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18150.         PK_NAME                = i.name,
  18151.         FK_NAME                = object_name(r.constid),
  18152.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18153.     from
  18154.         sysobjects o1, sysobjects o2,
  18155.         syscolumns c1, syscolumns c2,
  18156.         sysreferences r, sysindexes i
  18157.     where    
  18158.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18159.     and    o2.name = @fk_table_name
  18160.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18161.     and    o2.id = r.fkeyid
  18162.     and    r.keycnt >= 10
  18163.     and    o2.id = c2.id
  18164.     and    c2.colid = r.fkey10
  18165.     and r.rkeyid = o1.id
  18166.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18167.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18168.     and    o1.id = c1.id    
  18169.     and    c1.colid = r.rkey10
  18170.     and permissions(o1.id) <> 0
  18171.     and permissions(o2.id) <> 0
  18172.     and i.id = r.rkeyid
  18173.     and i.indid = r.rkeyindid            
  18174.     union    all
  18175.     select
  18176.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18177.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18178.         PK_TABLE_NAME         = o1.name,
  18179.         PK_COLUMN_NAME         = c1.name,
  18180.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18181.         PK_COLUMN_PROPID    = convert(int,null),
  18182.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18183.         FK_TABLE_SCHEMA     = user_name(o2.uid),
  18184.         FK_TABLE_NAME         = o2.name,
  18185.         FK_COLUMN_NAME         = c2.name,
  18186.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18187.         FK_COLUMN_PROPID    = convert(int,null),
  18188.         ORDINAL                = convert(int,11),
  18189.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18190.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18191.         PK_NAME                = i.name,
  18192.         FK_NAME                = object_name(r.constid),
  18193.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18194.     from
  18195.         sysobjects o1, sysobjects o2,
  18196.         syscolumns c1, syscolumns c2,
  18197.         sysreferences r, sysindexes i
  18198.     where    
  18199.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18200.     and    o2.name = @fk_table_name
  18201.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18202.     and    o2.id = r.fkeyid
  18203.     and    r.keycnt >= 11
  18204.     and    o2.id = c2.id
  18205.     and    c2.colid = r.fkey11
  18206.     and r.rkeyid = o1.id
  18207.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18208.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18209.     and    o1.id = c1.id    
  18210.     and    c1.colid = r.rkey11
  18211.     and permissions(o1.id) <> 0
  18212.     and permissions(o2.id) <> 0
  18213.     and i.id = r.rkeyid
  18214.     and i.indid = r.rkeyindid            
  18215.     union    all
  18216.     select
  18217.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18218.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18219.         PK_TABLE_NAME         = o1.name,
  18220.         PK_COLUMN_NAME         = c1.name,
  18221.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18222.         PK_COLUMN_PROPID    = convert(int,null),
  18223.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18224.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18225.         FK_TABLE_NAME         = o2.name,
  18226.         FK_COLUMN_NAME         = c2.name,
  18227.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18228.         FK_COLUMN_PROPID    = convert(int,null),
  18229.         ORDINAL                = convert(int,12),
  18230.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18231.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18232.         PK_NAME                = i.name,
  18233.         FK_NAME                = object_name(r.constid),
  18234.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18235.     from
  18236.         sysobjects o1, sysobjects o2,
  18237.         syscolumns c1, syscolumns c2,
  18238.         sysreferences r, sysindexes i
  18239.     where    
  18240.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18241.     and    o2.name = @fk_table_name
  18242.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18243.     and    o2.id = r.fkeyid
  18244.     and    r.keycnt >= 12
  18245.     and    o2.id = c2.id
  18246.     and    c2.colid = r.fkey12
  18247.     and r.rkeyid = o1.id
  18248.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18249.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18250.     and    o1.id = c1.id    
  18251.     and    c1.colid = r.rkey12
  18252.     and permissions(o1.id) <> 0
  18253.     and permissions(o2.id) <> 0
  18254.     and i.id = r.rkeyid
  18255.     and i.indid = r.rkeyindid            
  18256.     union    all
  18257.     select
  18258.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18259.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18260.         PK_TABLE_NAME         = o1.name,
  18261.         PK_COLUMN_NAME         = c1.name,
  18262.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18263.         PK_COLUMN_PROPID    = convert(int,null),
  18264.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18265.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18266.         FK_TABLE_NAME         = o2.name,
  18267.         FK_COLUMN_NAME         = c2.name,
  18268.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18269.         FK_COLUMN_PROPID    = convert(int,null),
  18270.         ORDINAL                = convert(int,13),
  18271.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18272.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18273.         PK_NAME                = i.name,
  18274.         FK_NAME                = object_name(r.constid),
  18275.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18276.     from
  18277.         sysobjects o1, sysobjects o2,
  18278.         syscolumns c1, syscolumns c2,
  18279.         sysreferences r, sysindexes i
  18280.     where    
  18281.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18282.     and    o2.name = @fk_table_name
  18283.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18284.     and    o2.id = r.fkeyid
  18285.     and    r.keycnt >= 13
  18286.     and    o2.id = c2.id
  18287.     and    c2.colid = r.fkey13
  18288.     and r.rkeyid = o1.id
  18289.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18290.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18291.     and    o1.id = c1.id    
  18292.     and    c1.colid = r.rkey13
  18293.     and permissions(o1.id) <> 0
  18294.     and permissions(o2.id) <> 0
  18295.     and i.id = r.rkeyid
  18296.     and i.indid = r.rkeyindid            
  18297.     union    all
  18298.     select
  18299.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18300.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18301.         PK_TABLE_NAME         = o1.name,
  18302.         PK_COLUMN_NAME         = c1.name,
  18303.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18304.         PK_COLUMN_PROPID    = convert(int,null),
  18305.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18306.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18307.         FK_TABLE_NAME         = o2.name,
  18308.         FK_COLUMN_NAME         = c2.name,
  18309.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18310.         FK_COLUMN_PROPID    = convert(int,null),
  18311.         ORDINAL                = convert(int,14),
  18312.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18313.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18314.         PK_NAME                = i.name,
  18315.         FK_NAME                = object_name(r.constid),
  18316.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18317.     from
  18318.         sysobjects o1, sysobjects o2,
  18319.         syscolumns c1, syscolumns c2,
  18320.         sysreferences r, sysindexes i
  18321.     where    
  18322.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18323.     and    o2.name = @fk_table_name
  18324.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18325.     and    o2.id = r.fkeyid
  18326.     and    r.keycnt >= 14
  18327.     and    o2.id = c2.id
  18328.     and    c2.colid = r.fkey14
  18329.     and r.rkeyid = o1.id
  18330.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18331.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18332.     and    o1.id = c1.id    
  18333.     and    c1.colid = r.rkey14
  18334.     and permissions(o1.id) <> 0
  18335.     and permissions(o2.id) <> 0
  18336.     and i.id = r.rkeyid
  18337.     and i.indid = r.rkeyindid            
  18338.     union    all
  18339.     select
  18340.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18341.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18342.         PK_TABLE_NAME         = o1.name,
  18343.         PK_COLUMN_NAME         = c1.name,
  18344.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18345.         PK_COLUMN_PROPID    = convert(int,null),
  18346.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18347.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18348.         FK_TABLE_NAME         = o2.name,
  18349.         FK_COLUMN_NAME         = c2.name,
  18350.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18351.         FK_COLUMN_PROPID    = convert(int,null),
  18352.         ORDINAL                = convert(int,15),
  18353.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18354.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18355.         PK_NAME                = i.name,
  18356.         FK_NAME                = object_name(r.constid),
  18357.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  18358.     from
  18359.         sysobjects o1, sysobjects o2,
  18360.         syscolumns c1, syscolumns c2,
  18361.         sysreferences r, sysindexes i
  18362.     where    
  18363.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18364.     and    o2.name = @fk_table_name
  18365.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18366.     and    o2.id = r.fkeyid
  18367.     and    r.keycnt >= 15
  18368.     and    o2.id = c2.id
  18369.     and    c2.colid = r.fkey15
  18370.     and r.rkeyid = o1.id
  18371.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18372.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18373.     and    o1.id = c1.id    
  18374.     and    c1.colid = r.rkey15
  18375.     and permissions(o1.id) <> 0
  18376.     and permissions(o2.id) <> 0
  18377.     and i.id = r.rkeyid
  18378.     and i.indid = r.rkeyindid            
  18379.     union    all
  18380.     select
  18381.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18382.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18383.         PK_TABLE_NAME         = o1.name,
  18384.         PK_COLUMN_NAME         = c1.name,
  18385.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18386.         PK_COLUMN_PROPID    = convert(int,null),
  18387.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18388.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18389.         FK_TABLE_NAME         = o2.name,
  18390.         FK_COLUMN_NAME         = c2.name,
  18391.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18392.         FK_COLUMN_PROPID    = convert(int,null),
  18393.         ORDINAL                = convert(int,16),
  18394.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18395.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18396.         PK_NAME                = i.name,
  18397.         FK_NAME                = object_name(r.constid),
  18398.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  18399.     from
  18400.         sysobjects o1, sysobjects o2,
  18401.         syscolumns c1, syscolumns c2,
  18402.         sysreferences r, sysindexes i
  18403.     where    
  18404.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18405.     and    o2.name = @fk_table_name
  18406.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18407.     and    o2.id = r.fkeyid
  18408.     and    r.keycnt >= 16
  18409.     and    o2.id = c2.id
  18410.     and    c2.colid = r.fkey16
  18411.     and r.rkeyid = o1.id
  18412.     and    (@pk_table_name is null or @pk_table_name = o1.name)
  18413.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18414.     and    o1.id = c1.id    
  18415.     and    c1.colid = r.rkey16
  18416.     and permissions(o1.id) <> 0
  18417.     and permissions(o2.id) <> 0
  18418.     and i.id = r.rkeyid
  18419.     and i.indid = r.rkeyindid        
  18420.     order by 8,9,2,3,13
  18421. go
  18422. dump tran master with no_log
  18423. go
  18424. CREATE PROCEDURE sp_foreign_keys_rowset;3
  18425.     (
  18426.     @pk_table_schema    sysname = null,
  18427.     @pk_table_catalog    sysname = null,
  18428.     @fk_table_schema    sysname = null,
  18429.     @fk_table_catalog    sysname = null
  18430.     )
  18431. as
  18432.     select
  18433.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18434.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18435.         PK_TABLE_NAME         = o1.name,
  18436.         PK_COLUMN_NAME         = c1.name,
  18437.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18438.         PK_COLUMN_PROPID    = convert(int,null),
  18439.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18440.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18441.         FK_TABLE_NAME         = o2.name,
  18442.         FK_COLUMN_NAME         = c2.name,
  18443.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18444.         FK_COLUMN_PROPID    = convert(int,null),
  18445.         ORDINAL                = convert(int,1),
  18446.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18447.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18448.         PK_NAME                = i.name,
  18449.         FK_NAME                = object_name(r.constid),
  18450.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/        
  18451.     from
  18452.         sysobjects o1, sysobjects o2,
  18453.         syscolumns c1, syscolumns c2,
  18454.         sysreferences r, sysindexes i
  18455.     where    
  18456.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18457.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18458.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18459.     and    o1.id = r.rkeyid
  18460.     and    o1.id = c1.id
  18461.     and    c1.colid = r.rkey1
  18462.     and r.fkeyid = o2.id
  18463.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18464.     and    o2.id = c2.id    
  18465.     and    c2.colid = r.fkey1
  18466.     and permissions(o1.id) <> 0
  18467.     and permissions(o2.id) <> 0
  18468.     and i.id = r.rkeyid
  18469.     and i.indid = r.rkeyindid        
  18470.     union all
  18471.     select
  18472.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18473.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18474.         PK_TABLE_NAME         = o1.name,
  18475.         PK_COLUMN_NAME         = c1.name,
  18476.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18477.         PK_COLUMN_PROPID    = convert(int,null),
  18478.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18479.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18480.         FK_TABLE_NAME         = o2.name,
  18481.         FK_COLUMN_NAME         = c2.name,
  18482.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18483.         FK_COLUMN_PROPID    = convert(int,null),
  18484.         ORDINAL                = convert(int,2),
  18485.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18486.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18487.         PK_NAME                = i.name,
  18488.         FK_NAME                = object_name(r.constid),
  18489.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18490.     from
  18491.         sysobjects o1, sysobjects o2,
  18492.         syscolumns c1, syscolumns c2,
  18493.         sysreferences r, sysindexes i
  18494.     where    
  18495.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18496.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18497.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18498.     and    o1.id = r.rkeyid
  18499.     and    r.keycnt >= 2
  18500.     and    o1.id = c1.id
  18501.     and    c1.colid = r.rkey2
  18502.     and r.fkeyid = o2.id
  18503.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18504.     and    o2.id = c2.id    
  18505.     and    c2.colid = r.fkey2
  18506.     and permissions(o1.id) <> 0
  18507.     and permissions(o2.id) <> 0
  18508.     and i.id = r.rkeyid
  18509.     and i.indid = r.rkeyindid        
  18510.     union all
  18511.     select
  18512.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18513.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18514.         PK_TABLE_NAME         = o1.name,
  18515.         PK_COLUMN_NAME         = c1.name,
  18516.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18517.         PK_COLUMN_PROPID    = convert(int,null),
  18518.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18519.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18520.         FK_TABLE_NAME         = o2.name,
  18521.         FK_COLUMN_NAME         = c2.name,
  18522.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18523.         FK_COLUMN_PROPID    = convert(int,null),
  18524.         ORDINAL                = convert(int,3),
  18525.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18526.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18527.         PK_NAME                = i.name,
  18528.         FK_NAME                = object_name(r.constid),
  18529.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18530.     from
  18531.         sysobjects o1, sysobjects o2,
  18532.         syscolumns c1, syscolumns c2,
  18533.         sysreferences r, sysindexes i
  18534.     where    
  18535.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18536.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18537.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18538.     and    o1.id = r.rkeyid
  18539.     and    r.keycnt >= 3
  18540.     and    o1.id = c1.id
  18541.     and    c1.colid = r.rkey3
  18542.     and r.fkeyid = o2.id
  18543.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18544.     and    o2.id = c2.id    
  18545.     and    c2.colid = r.fkey3
  18546.     and permissions(o1.id) <> 0
  18547.     and permissions(o2.id) <> 0
  18548.     and i.id = r.rkeyid
  18549.     and i.indid = r.rkeyindid        
  18550.     union all
  18551.     select
  18552.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18553.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18554.         PK_TABLE_NAME         = o1.name,
  18555.         PK_COLUMN_NAME         = c1.name,
  18556.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18557.         PK_COLUMN_PROPID    = convert(int,null),
  18558.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18559.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18560.         FK_TABLE_NAME         = o2.name,
  18561.         FK_COLUMN_NAME         = c2.name,
  18562.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18563.         FK_COLUMN_PROPID    = convert(int,null),
  18564.         ORDINAL                = convert(int,4),
  18565.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18566.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18567.         PK_NAME                = i.name,
  18568.         FK_NAME                = object_name(r.constid),
  18569.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18570.     from
  18571.         sysobjects o1, sysobjects o2,
  18572.         syscolumns c1, syscolumns c2,
  18573.         sysreferences r, sysindexes i
  18574.     where    
  18575.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18576.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18577.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18578.     and    o1.id = r.rkeyid
  18579.     and    r.keycnt >= 4
  18580.     and    o1.id = c1.id
  18581.     and    c1.colid = r.rkey4
  18582.     and r.fkeyid = o2.id
  18583.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18584.     and    o2.id = c2.id    
  18585.     and    c2.colid = r.fkey4
  18586.     and permissions(o1.id) <> 0
  18587.     and permissions(o2.id) <> 0
  18588.     and i.id = r.rkeyid
  18589.     and i.indid = r.rkeyindid        
  18590.     union all
  18591.     select
  18592.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18593.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18594.         PK_TABLE_NAME         = o1.name,
  18595.         PK_COLUMN_NAME         = c1.name,
  18596.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18597.         PK_COLUMN_PROPID    = convert(int,null),
  18598.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18599.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18600.         FK_TABLE_NAME         = o2.name,
  18601.         FK_COLUMN_NAME         = c2.name,
  18602.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18603.         FK_COLUMN_PROPID    = convert(int,null),
  18604.         ORDINAL                = convert(int,5),
  18605.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18606.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18607.         PK_NAME                = i.name,
  18608.         FK_NAME                = object_name(r.constid),
  18609.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18610.     from
  18611.         sysobjects o1, sysobjects o2,
  18612.         syscolumns c1, syscolumns c2,
  18613.         sysreferences r, sysindexes i
  18614.     where    
  18615.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18616.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18617.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18618.     and    o1.id = r.rkeyid
  18619.     and    r.keycnt >= 5
  18620.     and    o1.id = c1.id
  18621.     and    c1.colid = r.rkey5
  18622.     and r.fkeyid = o2.id
  18623.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18624.     and    o2.id = c2.id    
  18625.     and    c2.colid = r.fkey5
  18626.     and permissions(o1.id) <> 0
  18627.     and permissions(o2.id) <> 0
  18628.     and i.id = r.rkeyid
  18629.     and i.indid = r.rkeyindid        
  18630.     union all
  18631.     select
  18632.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18633.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18634.         PK_TABLE_NAME         = o1.name,
  18635.         PK_COLUMN_NAME         = c1.name,
  18636.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18637.         PK_COLUMN_PROPID    = convert(int,null),
  18638.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18639.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18640.         FK_TABLE_NAME         = o2.name,
  18641.         FK_COLUMN_NAME         = c2.name,
  18642.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18643.         FK_COLUMN_PROPID    = convert(int,null),
  18644.         ORDINAL                = convert(int,6),
  18645.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18646.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18647.         PK_NAME                = i.name,
  18648.         FK_NAME                = object_name(r.constid),
  18649.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18650.     from
  18651.         sysobjects o1, sysobjects o2,
  18652.         syscolumns c1, syscolumns c2,
  18653.         sysreferences r, sysindexes i
  18654.     where    
  18655.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18656.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18657.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18658.     and    o1.id = r.rkeyid
  18659.     and    r.keycnt >= 6
  18660.     and    o1.id = c1.id
  18661.     and    c1.colid = r.rkey6
  18662.     and r.fkeyid = o2.id
  18663.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18664.     and    o2.id = c2.id    
  18665.     and    c2.colid = r.fkey6
  18666.     and permissions(o1.id) <> 0
  18667.     and permissions(o2.id) <> 0
  18668.     and i.id = r.rkeyid
  18669.     and i.indid = r.rkeyindid        
  18670.     union all
  18671.     select
  18672.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18673.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18674.         PK_TABLE_NAME         = o1.name,
  18675.         PK_COLUMN_NAME         = c1.name,
  18676.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18677.         PK_COLUMN_PROPID    = convert(int,null),
  18678.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18679.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18680.         FK_TABLE_NAME         = o2.name,
  18681.         FK_COLUMN_NAME         = c2.name,
  18682.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18683.         FK_COLUMN_PROPID    = convert(int,null),
  18684.         ORDINAL                = convert(int,7),
  18685.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18686.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18687.         PK_NAME                = i.name,
  18688.         FK_NAME                = object_name(r.constid),
  18689.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18690.     from
  18691.         sysobjects o1, sysobjects o2,
  18692.         syscolumns c1, syscolumns c2,
  18693.         sysreferences r, sysindexes i
  18694.     where    
  18695.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18696.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18697.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18698.     and    o1.id = r.rkeyid
  18699.     and    r.keycnt >= 7
  18700.     and    o1.id = c1.id
  18701.     and    c1.colid = r.rkey7
  18702.     and r.fkeyid = o2.id
  18703.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18704.     and    o2.id = c2.id    
  18705.     and    c2.colid = r.fkey7
  18706.     and permissions(o1.id) <> 0
  18707.     and permissions(o2.id) <> 0
  18708.     and i.id = r.rkeyid
  18709.     and i.indid = r.rkeyindid        
  18710.     union all
  18711.     select
  18712.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18713.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18714.         PK_TABLE_NAME         = o1.name,
  18715.         PK_COLUMN_NAME         = c1.name,
  18716.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18717.         PK_COLUMN_PROPID    = convert(int,null),
  18718.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18719.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18720.         FK_TABLE_NAME         = o2.name,
  18721.         FK_COLUMN_NAME         = c2.name,
  18722.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18723.         FK_COLUMN_PROPID    = convert(int,null),
  18724.         ORDINAL                = convert(int,8),
  18725.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18726.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18727.         PK_NAME                = i.name,
  18728.         FK_NAME                = object_name(r.constid),
  18729.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18730.     from
  18731.         sysobjects o1, sysobjects o2,
  18732.         syscolumns c1, syscolumns c2,
  18733.         sysreferences r, sysindexes i
  18734.     where    
  18735.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18736.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18737.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18738.     and    o1.id = r.rkeyid
  18739.     and    r.keycnt >= 8
  18740.     and    o1.id = c1.id
  18741.     and    c1.colid = r.rkey8
  18742.     and r.fkeyid = o2.id
  18743.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18744.     and    o2.id = c2.id    
  18745.     and    c2.colid = r.fkey8
  18746.     and permissions(o1.id) <> 0
  18747.     and permissions(o2.id) <> 0
  18748.     and i.id = r.rkeyid
  18749.     and i.indid = r.rkeyindid        
  18750.     union all
  18751.     select
  18752.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18753.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18754.         PK_TABLE_NAME         = o1.name,
  18755.         PK_COLUMN_NAME         = c1.name,
  18756.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18757.         PK_COLUMN_PROPID    = convert(int,null),
  18758.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18759.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18760.         FK_TABLE_NAME         = o2.name,
  18761.         FK_COLUMN_NAME         = c2.name,
  18762.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18763.         FK_COLUMN_PROPID    = convert(int,null),
  18764.         ORDINAL                = convert(int,9),
  18765.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18766.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18767.         PK_NAME                = i.name,
  18768.         FK_NAME                = object_name(r.constid),
  18769.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18770.     from
  18771.         sysobjects o1, sysobjects o2,
  18772.         syscolumns c1, syscolumns c2,
  18773.         sysreferences r, sysindexes i
  18774.     where    
  18775.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18776.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18777.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18778.     and    o1.id = r.rkeyid
  18779.     and    r.keycnt >= 9
  18780.     and    o1.id = c1.id
  18781.     and    c1.colid = r.rkey9
  18782.     and r.fkeyid = o2.id
  18783.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18784.     and    o2.id = c2.id    
  18785.     and    c2.colid = r.fkey9
  18786.     and permissions(o1.id) <> 0
  18787.     and permissions(o2.id) <> 0
  18788.     and i.id = r.rkeyid
  18789.     and i.indid = r.rkeyindid        
  18790.     union all
  18791.     select
  18792.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18793.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18794.         PK_TABLE_NAME         = o1.name,
  18795.         PK_COLUMN_NAME         = c1.name,
  18796.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18797.         PK_COLUMN_PROPID    = convert(int,null),
  18798.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18799.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18800.         FK_TABLE_NAME         = o2.name,
  18801.         FK_COLUMN_NAME         = c2.name,
  18802.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18803.         FK_COLUMN_PROPID    = convert(int,null),
  18804.         ORDINAL                = convert(int,10),
  18805.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18806.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18807.         PK_NAME                = i.name,
  18808.         FK_NAME                = object_name(r.constid),
  18809.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18810.     from
  18811.         sysobjects o1, sysobjects o2,
  18812.         syscolumns c1, syscolumns c2,
  18813.         sysreferences r, sysindexes i
  18814.     where    
  18815.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18816.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18817.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18818.     and    o1.id = r.rkeyid
  18819.     and    r.keycnt >= 10
  18820.     and    o1.id = c1.id
  18821.     and    c1.colid = r.rkey10
  18822.     and r.fkeyid = o2.id
  18823.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18824.     and    o2.id = c2.id    
  18825.     and    c2.colid = r.fkey10
  18826.     and permissions(o1.id) <> 0
  18827.     and permissions(o2.id) <> 0
  18828.     and i.id = r.rkeyid
  18829.     and i.indid = r.rkeyindid        
  18830.     union all
  18831.     select
  18832.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18833.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18834.         PK_TABLE_NAME         = o1.name,
  18835.         PK_COLUMN_NAME         = c1.name,
  18836.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18837.         PK_COLUMN_PROPID    = convert(int,null),
  18838.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18839.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18840.         FK_TABLE_NAME         = o2.name,
  18841.         FK_COLUMN_NAME         = c2.name,
  18842.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18843.         FK_COLUMN_PROPID    = convert(int,null),
  18844.         ORDINAL                = convert(int,11),
  18845.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18846.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18847.         PK_NAME                = i.name,
  18848.         FK_NAME                = object_name(r.constid),
  18849.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18850.     from
  18851.         sysobjects o1, sysobjects o2,
  18852.         syscolumns c1, syscolumns c2,
  18853.         sysreferences r, sysindexes i
  18854.     where    
  18855.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18856.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18857.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18858.     and    o1.id = r.rkeyid
  18859.     and    r.keycnt >= 11
  18860.     and    o1.id = c1.id
  18861.     and    c1.colid = r.rkey11
  18862.     and r.fkeyid = o2.id
  18863.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18864.     and    o2.id = c2.id    
  18865.     and    c2.colid = r.fkey11
  18866.     and permissions(o1.id) <> 0
  18867.     and permissions(o2.id) <> 0
  18868.     and i.id = r.rkeyid
  18869.     and i.indid = r.rkeyindid        
  18870.     union all
  18871.     select
  18872.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18873.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18874.         PK_TABLE_NAME         = o1.name,
  18875.         PK_COLUMN_NAME         = c1.name,
  18876.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18877.         PK_COLUMN_PROPID    = convert(int,null),
  18878.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18879.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18880.         FK_TABLE_NAME         = o2.name,
  18881.         FK_COLUMN_NAME         = c2.name,
  18882.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18883.         FK_COLUMN_PROPID    = convert(int,null),
  18884.         ORDINAL                = convert(int,12),
  18885.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18886.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18887.         PK_NAME                = i.name,
  18888.         FK_NAME                = object_name(r.constid),
  18889.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18890.     from
  18891.         sysobjects o1, sysobjects o2,
  18892.         syscolumns c1, syscolumns c2,
  18893.         sysreferences r, sysindexes i
  18894.     where    
  18895.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18896.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18897.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18898.     and    o1.id = r.rkeyid
  18899.     and    r.keycnt >= 12
  18900.     and    o1.id = c1.id
  18901.     and    c1.colid = r.rkey12
  18902.     and r.fkeyid = o2.id
  18903.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18904.     and    o2.id = c2.id    
  18905.     and    c2.colid = r.fkey12
  18906.     and permissions(o1.id) <> 0
  18907.     and permissions(o2.id) <> 0
  18908.     and i.id = r.rkeyid
  18909.     and i.indid = r.rkeyindid        
  18910.     union all
  18911.     select
  18912.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18913.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18914.         PK_TABLE_NAME         = o1.name,
  18915.         PK_COLUMN_NAME         = c1.name,
  18916.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18917.         PK_COLUMN_PROPID    = convert(int,null),
  18918.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18919.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18920.         FK_TABLE_NAME         = o2.name,
  18921.         FK_COLUMN_NAME         = c2.name,
  18922.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18923.         FK_COLUMN_PROPID    = convert(int,null),
  18924.         ORDINAL                = convert(int,13),
  18925.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18926.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18927.         PK_NAME                = i.name,
  18928.         FK_NAME                = object_name(r.constid),
  18929.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18930.     from
  18931.         sysobjects o1, sysobjects o2,
  18932.         syscolumns c1, syscolumns c2,
  18933.         sysreferences r, sysindexes i
  18934.     where    
  18935.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18936.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18937.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18938.     and    o1.id = r.rkeyid
  18939.     and    r.keycnt >= 13
  18940.     and    o1.id = c1.id
  18941.     and    c1.colid = r.rkey13
  18942.     and r.fkeyid = o2.id
  18943.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18944.     and    o2.id = c2.id    
  18945.     and    c2.colid = r.fkey13
  18946.     and permissions(o1.id) <> 0
  18947.     and permissions(o2.id) <> 0
  18948.     and i.id = r.rkeyid
  18949.     and i.indid = r.rkeyindid        
  18950.     union all
  18951.     select
  18952.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18953.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18954.         PK_TABLE_NAME         = o1.name,
  18955.         PK_COLUMN_NAME         = c1.name,
  18956.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18957.         PK_COLUMN_PROPID    = convert(int,null),
  18958.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18959.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  18960.         FK_TABLE_NAME         = o2.name,
  18961.         FK_COLUMN_NAME         = c2.name,
  18962.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18963.         FK_COLUMN_PROPID    = convert(int,null),
  18964.         ORDINAL                = convert(int,14),
  18965.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18966.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  18967.         PK_NAME                = i.name,
  18968.         FK_NAME                = object_name(r.constid),
  18969.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  18970.     from
  18971.         sysobjects o1, sysobjects o2,
  18972.         syscolumns c1, syscolumns c2,
  18973.         sysreferences r, sysindexes i
  18974.     where    
  18975.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  18976.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  18977.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  18978.     and    o1.id = r.rkeyid
  18979.     and    r.keycnt >= 14
  18980.     and    o1.id = c1.id
  18981.     and    c1.colid = r.rkey14
  18982.     and r.fkeyid = o2.id
  18983.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  18984.     and    o2.id = c2.id    
  18985.     and    c2.colid = r.fkey14
  18986.     and permissions(o1.id) <> 0
  18987.     and permissions(o2.id) <> 0
  18988.     and i.id = r.rkeyid
  18989.     and i.indid = r.rkeyindid        
  18990.     union all
  18991.     select
  18992.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  18993.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  18994.         PK_TABLE_NAME         = o1.name,
  18995.         PK_COLUMN_NAME         = c1.name,
  18996.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  18997.         PK_COLUMN_PROPID    = convert(int,null),
  18998.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  18999.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  19000.         FK_TABLE_NAME         = o2.name,
  19001.         FK_COLUMN_NAME         = c2.name,
  19002.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  19003.         FK_COLUMN_PROPID    = convert(int,null),
  19004.         ORDINAL                = convert(int,15),
  19005.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19006.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19007.         PK_NAME                = i.name,
  19008.         FK_NAME                = object_name(r.constid),
  19009.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  19010.     from
  19011.         sysobjects o1, sysobjects o2,
  19012.         syscolumns c1, syscolumns c2,
  19013.         sysreferences r, sysindexes i
  19014.     where    
  19015.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  19016.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  19017.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  19018.     and    o1.id = r.rkeyid
  19019.     and    r.keycnt >= 15
  19020.     and    o1.id = c1.id
  19021.     and    c1.colid = r.rkey15
  19022.     and r.fkeyid = o2.id
  19023.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  19024.     and    o2.id = c2.id    
  19025.     and    c2.colid = r.fkey15
  19026.     and permissions(o1.id) <> 0
  19027.     and permissions(o2.id) <> 0
  19028.     and i.id = r.rkeyid
  19029.     and i.indid = r.rkeyindid        
  19030.     union all
  19031.     select
  19032.         PK_TABLE_CATALOG    = db_name(r.rkeydbid),
  19033.         PK_TABLE_SCHEMA        = user_name(o1.uid),
  19034.         PK_TABLE_NAME         = o1.name,
  19035.         PK_COLUMN_NAME         = c1.name,
  19036.         PK_COLUMN_GUID        = convert(uniqueidentifier,null),
  19037.         PK_COLUMN_PROPID    = convert(int,null),
  19038.         FK_TABLE_CATALOG    = db_name(r.fkeydbid),
  19039.         FK_TABLE_SCHEMA        = user_name(o2.uid),
  19040.         FK_TABLE_NAME         = o2.name,
  19041.         FK_COLUMN_NAME         = c2.name,
  19042.         FK_COLUMN_GUID        = convert(uniqueidentifier,null),
  19043.         FK_COLUMN_PROPID    = convert(int,null),
  19044.         ORDINAL                = convert(int,16),
  19045.         UPDATE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsUpdateCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19046.         DELETE_RULE = CASE WHEN (ObjectProperty(r.constid, 'CnstIsDeleteCascade')=1) THEN N'CASCADE' ELSE N'NO ACTION' END,
  19047.         PK_NAME                = i.name,
  19048.         FK_NAME                = object_name(r.constid),
  19049.         DEFERRABILITY        = convert(smallint, 3) /*DBPROPVAL_DF_NOT_DEFERRABLE*/
  19050.     from
  19051.         sysobjects o1, sysobjects o2,
  19052.         syscolumns c1, syscolumns c2,
  19053.         sysreferences r, sysindexes i
  19054.     where    
  19055.         (@pk_table_catalog is null or @pk_table_catalog = db_name())
  19056.     and     (@fk_table_catalog is null or @fk_table_catalog = db_name())
  19057.     and    (@pk_table_schema is null or @pk_table_schema = user_name(o1.uid))
  19058.     and    o1.id = r.rkeyid
  19059.     and    o1.id = c1.id
  19060.     and    r.keycnt >= 16
  19061.     and    c1.colid = r.rkey16
  19062.     and r.fkeyid = o2.id
  19063.     and    (@fk_table_schema is null or @fk_table_schema = user_name(o2.uid))
  19064.     and    o2.id = c2.id    
  19065.     and    c2.colid = r.fkey16
  19066.     and permissions(o1.id) <> 0
  19067.     and permissions(o2.id) <> 0
  19068.     and i.id = r.rkeyid
  19069.     and i.indid = r.rkeyindid        
  19070.     order by 8,9,2,3,13
  19071. go
  19072. dump tran master with no_log
  19073. go
  19074. create procedure sp_foreign_keys_rowset;5
  19075.     (
  19076.     @server_name        sysname,
  19077.     @pk_catalog_name    sysname = null,
  19078.     @fk_catalog_name    sysname = null,
  19079.     @pk_table_name        sysname = null,
  19080.     @pk_table_schema    sysname = null,
  19081.     @fk_table_name        sysname = null,
  19082.     @fk_table_schema    sysname = null
  19083.     )
  19084. as
  19085.     select
  19086.         PK_TABLE_CATALOG,
  19087.         PK_TABLE_SCHEMA,    
  19088.         PK_TABLE_NAME,     
  19089.         PK_COLUMN_NAME,     
  19090.         PK_COLUMN_GUID,    
  19091.         PK_COLUMN_PROPID,
  19092.         FK_TABLE_CATALOG,
  19093.         FK_TABLE_SCHEMA, 
  19094.         FK_TABLE_NAME,     
  19095.         FK_COLUMN_NAME,     
  19096.         FK_COLUMN_GUID,    
  19097.         FK_COLUMN_PROPID,
  19098.         ORDINAL,        
  19099.         UPDATE_RULE,    
  19100.         DELETE_RULE
  19101.         --PK_NAME,        
  19102.         --FK_NAME,
  19103.         --DEFERRABILITY    
  19104.     from master.dbo.SYSREMOTE_FOREIGN_KEYS <
  19105.                 @server_name,
  19106.                 @pk_catalog_name,
  19107.                 @pk_table_schema,
  19108.                 @pk_table_name,
  19109.                 @fk_catalog_name,
  19110.                 @fk_table_schema,
  19111.                 @fk_table_name >
  19112.     order by 7,8,9,1,2,3,13
  19113. go
  19114.  
  19115. grant execute on sp_foreign_keys_rowset to public
  19116. go
  19117.  
  19118. dump tran master with no_log
  19119. go
  19120. if (charindex('6.00', @@version) > 0)
  19121.     begin
  19122.     if (exists (select * from sysobjects
  19123.             where name = 'sp_foreign_keys_rowset' and type = 'P '))
  19124.         begin
  19125.         drop procedure sp_foreign_keys_rowset
  19126.         dump tran master with no_log
  19127.         end
  19128.     end
  19129. go
  19130.  
  19131.  
  19132. print ''
  19133. print 'creating sp_indexes_rowset'
  19134. go
  19135.  
  19136. /*    6.0 and 6.5 version */
  19137. create procedure sp_indexes_rowset
  19138.     (
  19139.     @table_name    varchar(255), 
  19140.     @index_name    varchar(255) = null,
  19141.     @table_schema    varchar(255) = null     
  19142.     )
  19143. as
  19144.     select    TABLE_CATALOG        = db_name(),
  19145.         TABLE_SCHEMA        = user_name(o.uid),
  19146.         TABLE_NAME        = o.name,
  19147.         INDEX_CATALOG        = db_name(),        
  19148.         INDEX_SCHEMA        = user_name(o.uid),
  19149.         INDEX_NAME        = x.name,
  19150.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19151.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19152.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19153.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19154.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19155.         INITIAL_SIZE        = convert(int,null),
  19156.         NULLS            = convert(int,null),
  19157.         SORT_BOOKMARKS        = convert(bit,0),
  19158.         AUTO_UPDATE        = convert(bit,1),
  19159.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19160.         ORDINAL_POSITION     = convert(int,
  19161.                        (
  19162.                         select count(*)
  19163.                         from syscolumns sc
  19164.                         where sc.id     =  c.id
  19165.                           AND sc.number =  c.number
  19166.                           AND sc.colid  <= c.colid
  19167.                         )),
  19168.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19169.         COLUMN_GUID        = convert(binary(16),null),
  19170.         COLUMN_PROPID        = convert(int,null),
  19171.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19172.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19173.         PAGES            = convert(int, x.dpages),
  19174.         FILTER_CONDITION    = convert(varchar(1),null),
  19175.         INTEGRATED        = convert(bit,(x.status & 16)/16)
  19176.         
  19177.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19178.     where    o.type in ('U')
  19179.     and     o.name = @table_name
  19180.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19181.     and    (@index_name is null or @index_name = x.name)
  19182.     and     x.id = o.id
  19183.     and     x.id = c.id
  19184.     and      c.colid < x.keycnt+(x.status&16)/16
  19185.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19186.     and     (
  19187.         suser_id() = 1     /* User is the System Administrator */
  19188.         or o.uid = user_id()     /* User created the object */
  19189.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19190.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19191.             from sysprotects p
  19192.             where p.id = o.id
  19193.             /* get rows for public,current user,user's group */
  19194.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19195.             /* check for SELECT,EXECUTE privilege */
  19196.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19197.             ) = 1    /* final magic...compare Grants      */
  19198.            )
  19199.     order by 8 desc, 4, 5, 6, 17
  19200. go
  19201. dump tran master with no_log
  19202. go
  19203. create procedure sp_indexes_rowset;2
  19204.     (
  19205.     @index_name    varchar(255) = null,
  19206.     @table_schema    varchar(255) = null     
  19207.     )
  19208. as
  19209.     select    TABLE_CATALOG        = db_name(),
  19210.         TABLE_SCHEMA        = user_name(o.uid),
  19211.         TABLE_NAME        = o.name,
  19212.         INDEX_CATALOG        = db_name(),        
  19213.         INDEX_SCHEMA        = user_name(o.uid),
  19214.         INDEX_NAME        = x.name,
  19215.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19216.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19217.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19218.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19219.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19220.         INITIAL_SIZE        = convert(int,null),
  19221.         NULLS            = convert(int,null),
  19222.         SORT_BOOKMARKS        = convert(bit,0),
  19223.         AUTO_UPDATE        = convert(bit,1),
  19224.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19225.         ORDINAL_POSITION     = convert(int,
  19226.                        (
  19227.                         select count(*)
  19228.                         from syscolumns sc
  19229.                         where sc.id     =  c.id
  19230.                           AND sc.number =  c.number
  19231.                           AND sc.colid  <= c.colid
  19232.                         )),
  19233.         COLUMN_NAME        = index_col(user_name(o.uid)+'.'+o.name, x.indid, c.colid),
  19234.         COLUMN_GUID        = convert(binary(16),null),
  19235.         COLUMN_PROPID        = convert(int,null),
  19236.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19237.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19238.         PAGES            = convert(int, x.dpages),
  19239.         FILTER_CONDITION    = convert(varchar(1),null),
  19240.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19241.         
  19242.     from    sysobjects o, sysindexes x, syscolumns c, sysusers u
  19243.     where    o.type in ('U')
  19244.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19245.     and    (@index_name is null or @index_name = x.name)
  19246.     and     x.id = o.id
  19247.     and     x.id = c.id
  19248.     and      c.colid < x.keycnt+(x.status&16)/16
  19249.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19250.     and     (
  19251.         suser_id() = 1     /* User is the System Administrator */
  19252.         or o.uid = user_id()     /* User created the object */
  19253.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19254.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19255.             from sysprotects p
  19256.             where p.id = o.id
  19257.             /* get rows for public,current user,user's group */
  19258.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19259.             /* check for SELECT,EXECUTE privilege */
  19260.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19261.             ) = 1    /* final magic...compare Grants      */
  19262.            )
  19263.     order by 8 desc, 4, 5, 6, 17
  19264. go
  19265. dump tran master with no_log
  19266. go
  19267.  
  19268. if (charindex('7.00', @@version) > 0 or
  19269.     charindex('8.00', @@version) > 0)
  19270.     drop procedure sp_indexes_rowset
  19271. else
  19272. begin
  19273.     print ''
  19274.     print ''
  19275.     print 'Warning:'
  19276.     print 'you are installing the stored procedures '
  19277.     print 'on a pre 7.0 SQL Server.'
  19278.     print 'Ignore the following errors.'
  19279. end
  19280. go
  19281.  
  19282. /*    7.0 version */
  19283. create procedure sp_indexes_rowset
  19284.     (
  19285.     @table_name    sysname, 
  19286.     @index_name    sysname = null,
  19287.     @table_schema    sysname = null     
  19288.     )
  19289. as
  19290.     select    TABLE_CATALOG        = db_name(),
  19291.         TABLE_SCHEMA        = user_name(o.uid),
  19292.         TABLE_NAME        = o.name,
  19293.         INDEX_CATALOG        = db_name(),        
  19294.         INDEX_SCHEMA        = user_name(o.uid),
  19295.         INDEX_NAME        = x.name,
  19296.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19297.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19298.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19299.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19300.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19301.         INITIAL_SIZE        = convert(int,null),
  19302.         NULLS            = convert(int,null),
  19303.         SORT_BOOKMARKS        = convert(bit,0),
  19304.         AUTO_UPDATE        = convert(bit,1),
  19305.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19306.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19307.         COLUMN_NAME        = c.name,
  19308.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19309.         COLUMN_PROPID        = convert(int,null),
  19310.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19311.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19312.         PAGES            = convert(int, x.dpages),
  19313.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19314.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19315.         
  19316.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19317.     where    o.type in ('U')
  19318.     and     o.name = @table_name
  19319.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19320.     and    (@index_name is null or @index_name = x.name)
  19321.     and    x.id = o.id
  19322.     and    o.id = c.id
  19323.     and    o.id = xk.id
  19324.     and    x.indid = xk.indid
  19325.     and    c.colid = xk.colid
  19326.     and    xk.keyno <= x.keycnt
  19327.     and    permissions(o.id, c.name) <> 0
  19328.     and     (x.status&32) = 0  -- No hypothetical indexes
  19329.     order by 8 desc, 4, 5, 6, 17
  19330. go
  19331. dump tran master with no_log
  19332. go
  19333. create procedure sp_indexes_rowset;2
  19334.     (
  19335.     @index_name    sysname = null,
  19336.     @table_schema    sysname = null     
  19337.     )
  19338. as
  19339.     select    TABLE_CATALOG        = db_name(),
  19340.         TABLE_SCHEMA        = user_name(o.uid),
  19341.         TABLE_NAME        = o.name,
  19342.         INDEX_CATALOG        = db_name(),        
  19343.         INDEX_SCHEMA        = user_name(o.uid),
  19344.         INDEX_NAME        = x.name,
  19345.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19346.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19347.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19348.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19349.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19350.         INITIAL_SIZE        = convert(int,null),
  19351.         NULLS            = convert(int,null),
  19352.         SORT_BOOKMARKS        = convert(bit,0),
  19353.         AUTO_UPDATE        = convert(bit,1),
  19354.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19355.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19356.         COLUMN_NAME        = c.name,
  19357.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19358.         COLUMN_PROPID        = convert(int,null),
  19359.         COLLATION    = convert(smallint, 1 /* DB_COLLATION_ASC */),
  19360.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19361.         PAGES            = convert(int, x.dpages),
  19362.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19363.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19364.         
  19365.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19366.     where    o.type in ('U')
  19367.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19368.     and    (@index_name is null or @index_name = x.name)
  19369.     and    x.id = o.id
  19370.     and    o.id = c.id
  19371.     and    o.id = xk.id
  19372.     and    x.indid = xk.indid
  19373.     and    c.colid = xk.colid
  19374.     and    xk.keyno <= x.keycnt
  19375.     and    permissions(o.id, c.name) <> 0
  19376.     and    (x.status&32) = 0  -- No hypothetical indexes
  19377.     order by 8 desc, 4, 5, 6, 17
  19378. go
  19379. dump tran master with no_log
  19380. go
  19381. create procedure sp_indexes_rowset;5
  19382.     (
  19383.     @table_server        sysname,
  19384.     @table_catalog        sysname = null,
  19385.     @table_name        sysname = null,
  19386.     @index_name        sysname = null,
  19387.     @table_schema        sysname = null
  19388.     )
  19389. as
  19390.     select
  19391.         TABLE_CATALOG,    
  19392.         TABLE_SCHEMA,    
  19393.         TABLE_NAME,    
  19394.         INDEX_CATALOG,        
  19395.         INDEX_SCHEMA,    
  19396.         INDEX_NAME,    
  19397.         PRIMARY_KEY,    
  19398.         "UNIQUE",    
  19399.         "CLUSTERED",    
  19400.         "TYPE",        
  19401.         FILL_FACTOR,    
  19402.         INITIAL_SIZE,    
  19403.         NULLS,        
  19404.         SORT_BOOKMARKS,    
  19405.         AUTO_UPDATE,    
  19406.         NULL_COLLATION,    
  19407.         ORDINAL_POSITION,
  19408.         COLUMN_NAME,    
  19409.         COLUMN_GUID,    
  19410.         COLUMN_PROPID,    
  19411.         COLLATION,    
  19412.         CARDINALITY,    
  19413.         PAGES,        
  19414.         FILTER_CONDITION
  19415.     --    INTEGRATED
  19416.     from master.dbo.SYSREMOTE_INDEXES <
  19417.                 @table_server,
  19418.                 @table_catalog,
  19419.                 @table_schema,
  19420.                 @index_name,
  19421.                 NULL,            /* TYPE (index type) */
  19422.                 @table_name >
  19423.     order by 8 desc, 4, 5, 6, 17
  19424. go
  19425.  
  19426. grant execute on sp_indexes_rowset to public
  19427. go
  19428.  
  19429. dump tran master with no_log
  19430. go
  19431.  
  19432. if (charindex('8.00', @@version) > 0)
  19433.     drop procedure sp_indexes_rowset
  19434. else
  19435. begin
  19436.     print ''
  19437.     print ''
  19438.     print 'Warning:'
  19439.     print 'you are installing the stored procedures '
  19440.     print 'on a pre 8.0 SQL Server.'
  19441.     print 'Ignore the following errors.'
  19442. end
  19443. go
  19444.  
  19445. /*    8.0 version */
  19446. create procedure sp_indexes_rowset
  19447.     (
  19448.     @table_name    sysname, 
  19449.     @index_name    sysname = null,
  19450.     @table_schema    sysname = null     
  19451.     )
  19452. as
  19453.     select    TABLE_CATALOG        = db_name(),
  19454.         TABLE_SCHEMA        = user_name(o.uid),
  19455.         TABLE_NAME        = o.name,
  19456.         INDEX_CATALOG        = db_name(),        
  19457.         INDEX_SCHEMA        = user_name(o.uid),
  19458.         INDEX_NAME        = x.name,
  19459.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19460.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19461.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19462.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19463.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19464.         INITIAL_SIZE        = convert(int,null),
  19465.         NULLS            = convert(int,null),
  19466.         SORT_BOOKMARKS        = convert(bit,0),
  19467.         AUTO_UPDATE        = convert(bit,1),
  19468.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19469.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19470.         COLUMN_NAME        = c.name,
  19471.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19472.         COLUMN_PROPID        = convert(int,null),
  19473.         COLLATION    = convert(smallint,
  19474.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19475.                         then 2        /* DB_COLLATION_DESC */ 
  19476.                         else 1        /* DB_COLLATION_ASC */ 
  19477.                         end),
  19478.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19479.         PAGES            = convert(int, x.dpages),
  19480.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19481.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19482.         
  19483.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19484.     where    o.type in ('U')
  19485.     and     o.name = @table_name
  19486.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19487.     and    (@index_name is null or @index_name = x.name)
  19488.     and    x.id = o.id
  19489.     and    o.id = c.id
  19490.     and    o.id = xk.id
  19491.     and    x.indid = xk.indid
  19492.     and    c.colid = xk.colid
  19493.     and    xk.keyno <= x.keycnt
  19494.     and    permissions(o.id, c.name) <> 0
  19495.     and     (x.status&32) = 0  -- No hypothetical indexes
  19496.     order by 8 desc, 4, 5, 6, 17
  19497. go
  19498. dump tran master with no_log
  19499. go
  19500. create procedure sp_indexes_rowset;2
  19501.     (
  19502.     @index_name    sysname = null,
  19503.     @table_schema    sysname = null     
  19504.     )
  19505. as
  19506.     select    TABLE_CATALOG        = db_name(),
  19507.         TABLE_SCHEMA        = user_name(o.uid),
  19508.         TABLE_NAME        = o.name,
  19509.         INDEX_CATALOG        = db_name(),        
  19510.         INDEX_SCHEMA        = user_name(o.uid),
  19511.         INDEX_NAME        = x.name,
  19512.         PRIMARY_KEY        = convert(bit,(x.status & 0x800)/0x800),
  19513.         "UNIQUE"        = convert(bit,(x.status & 2)/2),
  19514.         "CLUSTERED"        = convert(bit,(x.status & 16)/16),
  19515.         "TYPE"            = convert(smallint, 1 /*DBPROPVAL_IT_BTREE*/),
  19516.         FILL_FACTOR        = convert(int, x.OrigFillFactor),
  19517.         INITIAL_SIZE        = convert(int,null),
  19518.         NULLS            = convert(int,null),
  19519.         SORT_BOOKMARKS        = convert(bit,0),
  19520.         AUTO_UPDATE        = convert(bit,1),
  19521.         NULL_COLLATION        = convert(int,4 /*DBPROPVAL_NC_LOW*/),
  19522.         ORDINAL_POSITION     = convert(int, xk.keyno),
  19523.         COLUMN_NAME        = c.name,
  19524.         COLUMN_GUID        = convert(uniqueidentifier,null),
  19525.         COLUMN_PROPID        = convert(int,null),
  19526.         COLLATION    = convert(smallint,
  19527.                         case when indexkey_property(o.id, x.indid, xk.keyno, 'IsDescending') =1
  19528.                         then 2        /* DB_COLLATION_DESC */ 
  19529.                         else 1        /* DB_COLLATION_ASC */ 
  19530.                         end),
  19531.         CARDINALITY        = case when (x.status & 2) = 2 then x.rows else null end,
  19532.         PAGES            = convert(int, x.dpages),
  19533.         FILTER_CONDITION    = convert(nvarchar(1),null),
  19534.         INTEGRATED        = convert(bit,(x.status & 16)/16) 
  19535.         
  19536.     from    sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk
  19537.     where    o.type in ('U')
  19538.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19539.     and    (@index_name is null or @index_name = x.name)
  19540.     and    x.id = o.id
  19541.     and    o.id = c.id
  19542.     and    o.id = xk.id
  19543.     and    x.indid = xk.indid
  19544.     and    c.colid = xk.colid
  19545.     and    xk.keyno <= x.keycnt
  19546.     and    permissions(o.id, c.name) <> 0
  19547.     and    (x.status&32) = 0  -- No hypothetical indexes
  19548.     order by 8 desc, 4, 5, 6, 17
  19549. go
  19550. dump tran master with no_log
  19551. go
  19552. create procedure sp_indexes_rowset;5
  19553.     (
  19554.     @table_server        sysname,
  19555.     @table_catalog        sysname = null,
  19556.     @table_name        sysname = null,
  19557.     @index_name        sysname = null,
  19558.     @table_schema        sysname = null
  19559.     )
  19560. as
  19561.     select
  19562.         TABLE_CATALOG,    
  19563.         TABLE_SCHEMA,    
  19564.         TABLE_NAME,    
  19565.         INDEX_CATALOG,        
  19566.         INDEX_SCHEMA,    
  19567.         INDEX_NAME,    
  19568.         PRIMARY_KEY,    
  19569.         "UNIQUE",    
  19570.         "CLUSTERED",    
  19571.         "TYPE",        
  19572.         FILL_FACTOR,    
  19573.         INITIAL_SIZE,    
  19574.         NULLS,        
  19575.         SORT_BOOKMARKS,    
  19576.         AUTO_UPDATE,    
  19577.         NULL_COLLATION,    
  19578.         ORDINAL_POSITION,
  19579.         COLUMN_NAME,    
  19580.         COLUMN_GUID,    
  19581.         COLUMN_PROPID,    
  19582.         COLLATION,    
  19583.         CARDINALITY,    
  19584.         PAGES,        
  19585.         FILTER_CONDITION
  19586.     --    INTEGRATED
  19587.     from master.dbo.SYSREMOTE_INDEXES <
  19588.                 @table_server,
  19589.                 @table_catalog,
  19590.                 @table_schema,
  19591.                 @index_name,
  19592.                 NULL,            /* TYPE (index type) */
  19593.                 @table_name >
  19594.     order by 8 desc, 4, 5, 6, 17
  19595. go
  19596.  
  19597. grant execute on sp_indexes_rowset to public
  19598. go
  19599.  
  19600. dump tran master with no_log
  19601. go
  19602. if (charindex('6.00', @@version) > 0)
  19603.     begin
  19604.     if (exists (select * from sysobjects
  19605.             where name = 'sp_indexes_rowset' and type = 'P '))
  19606.         begin
  19607.         drop procedure sp_indexes_rowset
  19608.         dump tran master with no_log
  19609.         end
  19610.     end
  19611. go
  19612.  
  19613.  
  19614. print ''
  19615. print 'creating sp_primary_keys_rowset'
  19616. go
  19617.  
  19618. /*    Procedure for 6.0 and 6.5 servers */
  19619. create procedure sp_primary_keys_rowset
  19620.     (
  19621.        @table_name        varchar(255),
  19622.     @table_schema         varchar(244) = null
  19623.     )
  19624. as
  19625.     select    TABLE_CATALOG    = db_name(),
  19626.             TABLE_SCHEMA    = user_name(o.uid),
  19627.             TABLE_NAME        = o.name,    
  19628.             COLUMN_NAME        = c.name,
  19629.             COLUMN_GUID        = convert(binary(16),null),
  19630.             COLUMN_PROPID    = convert(int,null),
  19631.             ORDINAL            = convert(int,c1.colid),
  19632.             PK_NAME            = i.name
  19633.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  19634.     where    o.type in ('U')
  19635.     and     o.name = @table_name
  19636.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19637.     and     o.id = c.id
  19638.     and     o.id = i.id
  19639.     and     (i.status & 0x800) = 0x800
  19640.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19641.     and     c1.id = c.id
  19642.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19643.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19644.     and     (
  19645.         suser_id() = 1     /* User is the System Administrator */
  19646.         or o.uid = user_id()     /* User created the object */
  19647.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19648.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19649.             from sysprotects p
  19650.             where p.id = o.id
  19651.             /* get rows for public,current user,user's group */
  19652.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19653.             /* check for SELECT,EXECUTE privilege */
  19654.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19655.             ) = 1    /* final magic...compare Grants      */
  19656.            )
  19657.     order by 2, 3
  19658. go
  19659. dump tran master with no_log
  19660. go
  19661. create procedure sp_primary_keys_rowset;2
  19662.     (
  19663.      @table_schema         varchar(244) = null
  19664.     )
  19665. as
  19666.     select    TABLE_CATALOG    = db_name(),
  19667.             TABLE_SCHEMA    = user_name(o.uid),
  19668.             TABLE_NAME        = o.name,    
  19669.             COLUMN_NAME        = c.name,
  19670.             COLUMN_GUID        = convert(binary(16),null),
  19671.             COLUMN_PROPID    = convert(int,null),
  19672.             ORDINAL            = convert(int,c1.colid),
  19673.             PK_NAME            = i.name
  19674.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1, sysusers u
  19675.     where    o.type in ('U')
  19676.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19677.     and     o.id = c.id
  19678.     and     o.id = i.id
  19679.     and     (i.status & 0x800) = 0x800
  19680.     and     c.name = index_col(user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19681.     and     c1.id = c.id
  19682.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19683.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  19684.     and     (
  19685.         suser_id() = 1     /* User is the System Administrator */
  19686.         or o.uid = user_id()     /* User created the object */
  19687.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  19688.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  19689.             from sysprotects p
  19690.             where p.id = o.id
  19691.             /* get rows for public,current user,user's group */
  19692.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  19693.             /* check for SELECT,EXECUTE privilege */
  19694.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  19695.             ) = 1    /* final magic...compare Grants      */
  19696.            )
  19697.     order by 2, 3
  19698. go
  19699.  
  19700. if (charindex('7.00', @@version) = 0 and
  19701.     charindex('8.00', @@version) = 0)
  19702. begin
  19703.     print ''
  19704.     print ''
  19705.     print 'Warning:'
  19706.     print 'you are installing the stored procedures '
  19707.     print 'on a pre 8.0 SQL Server.'
  19708.     print 'Ignore the following errors.'
  19709. end
  19710. else
  19711.     drop proc sp_primary_keys_rowset
  19712. go
  19713.  
  19714. /*    Procedure for 8.0 servers */
  19715. create procedure sp_primary_keys_rowset
  19716.     (
  19717.        @table_name        sysname,
  19718.     @table_schema         sysname = null
  19719.     )
  19720. as
  19721.     select    TABLE_CATALOG    = db_name(),
  19722.             TABLE_SCHEMA    = user_name(o.uid),
  19723.             TABLE_NAME        = o.name,    
  19724.             COLUMN_NAME        = c.name,
  19725.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19726.             COLUMN_PROPID    = convert(int,null),
  19727.             ORDINAL            = convert(int,c1.colid),
  19728.             PK_NAME            = i.name
  19729.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19730.     where    o.type in ('U')
  19731.     and     o.name = @table_name
  19732.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19733.     and     o.id = c.id
  19734.     and     o.id = i.id
  19735.     and     (i.status & 0x800) = 0x800
  19736.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19737.     and     c1.id = c.id
  19738.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19739.     and    permissions(o.id) <> 0
  19740.     order by 2, 3
  19741. go
  19742. dump tran master with no_log
  19743. go
  19744. create procedure sp_primary_keys_rowset;2
  19745.     (
  19746.      @table_schema     sysname = null
  19747.     )
  19748. as
  19749.     select    TABLE_CATALOG    = db_name(),
  19750.             TABLE_SCHEMA    = user_name(o.uid),
  19751.             TABLE_NAME        = o.name,    
  19752.             COLUMN_NAME        = c.name,
  19753.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19754.             COLUMN_PROPID    = convert(int,null),
  19755.             ORDINAL            = convert(int,c1.colid),
  19756.             PK_NAME            = i.name
  19757.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19758.     where    o.type in ('U')
  19759.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19760.     and     o.id = c.id
  19761.     and     o.id = i.id
  19762.     and     (i.status & 0x800) = 0x800
  19763.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19764.     and     c1.id = c.id
  19765.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19766.     and    permissions(o.id) <> 0
  19767.     order by 2, 3
  19768. go
  19769. dump tran master with no_log
  19770. go
  19771. create procedure sp_primary_keys_rowset;3
  19772.     (
  19773.        @table_name        sysname = null,
  19774.     @table_schema         sysname = null
  19775.     )
  19776. as
  19777. IF @table_name is not NULL
  19778.     BEGIN
  19779.     select    TABLE_CATALOG    = db_name(),
  19780.             TABLE_SCHEMA    = user_name(o.uid),
  19781.             TABLE_NAME        = o.name,    
  19782.             COLUMN_NAME        = c.name,
  19783.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19784.             COLUMN_PROPID    = convert(int,null),
  19785.             ORDINAL            = convert(int,c1.colid),
  19786.             PK_NAME            = i.name
  19787.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19788.     where    o.type in ('U')
  19789.     and     o.name = @table_name
  19790.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19791.     and     o.id = c.id
  19792.     and     o.id = i.id
  19793.     and     (i.status & 0x800) = 0x800
  19794.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19795.     and     c1.id = c.id
  19796.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19797.     and    permissions(o.id) <> 0
  19798.     END
  19799. ELSE
  19800.     BEGIN
  19801.     select    TABLE_CATALOG    = db_name(),
  19802.             TABLE_SCHEMA    = user_name(o.uid),
  19803.             TABLE_NAME        = o.name,    
  19804.             COLUMN_NAME        = c.name,
  19805.             COLUMN_GUID        = convert(uniqueidentifier,null),
  19806.             COLUMN_PROPID    = convert(int,null),
  19807.             ORDINAL            = convert(int,c1.colid),
  19808.             PK_NAME            = i.name
  19809.     from    sysindexes i, syscolumns c, sysobjects o, syscolumns c1
  19810.     where    o.type in ('U')
  19811.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  19812.     and     o.id = c.id
  19813.     and     o.id = i.id
  19814.     and     (i.status & 0x800) = 0x800
  19815.     and     c.name = index_col (user_name(o.uid)+'.'+o.name, i.indid, c1.colid)
  19816.     and     c1.id = c.id
  19817.     and     c1.colid <= i.keycnt    /* create rows from 1 to keycnt */
  19818.     and    permissions(o.id) <> 0
  19819.     END
  19820. go
  19821. dump tran master with no_log
  19822. go
  19823. create procedure sp_primary_keys_rowset;5
  19824.     (
  19825.     @table_server        sysname,
  19826.     @table_catalog        sysname = null,
  19827.     @table_name            sysname = null,
  19828.     @table_schema        sysname = null
  19829.     )
  19830. as
  19831.     select
  19832.         TABLE_CATALOG,    
  19833.         TABLE_SCHEMA,    
  19834.         TABLE_NAME,    
  19835.         COLUMN_NAME,    
  19836.         COLUMN_GUID,    
  19837.         COLUMN_PROPID,    
  19838.         ORDINAL
  19839.         --PK_NAME        
  19840.     from master.dbo.SYSREMOTE_PRIMARY_KEYS <
  19841.                 @table_server,
  19842.                 @table_catalog,
  19843.                 @table_schema,
  19844.                 @table_name >
  19845.     order by 1,2,3
  19846. go
  19847.  
  19848. grant execute on sp_primary_keys_rowset to public
  19849. go
  19850.  
  19851. dump tran master with no_log
  19852. go
  19853. if (charindex('6.00', @@version) > 0)
  19854.     begin
  19855.     if (exists (select * from sysobjects
  19856.             where name = 'sp_primary_keys_rowset' and type = 'P '))
  19857.         begin
  19858.         drop procedure sp_primary_keys_rowset
  19859.         dump tran master with no_log
  19860.         end
  19861.     end
  19862. go
  19863.  
  19864.  
  19865. print ''
  19866. print 'creating sp_provider_types_rowset'
  19867. go
  19868.  
  19869. /*    Procedure for 6.0 and 6.50 servers */
  19870. create proc sp_provider_types_rowset
  19871.     (
  19872.     @data_type     smallint = null,
  19873.     @best_match    tinyint  = null
  19874.     )
  19875. as
  19876.     select
  19877.         TYPE_NAME         = case when t.usertype = 80 then t.name 
  19878.                         else d.type_name 
  19879.                         end,
  19880.         DATA_TYPE         = d.oledb_data_type,                            
  19881.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19882.                             then @@max_precision
  19883.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  19884.         LITERAL_PREFIX         = d.literal_prefix,
  19885.         LITERAL_SUFFIX         = d.literal_suffix,
  19886.         CREATE_PARAMS         = convert(varchar(32),e.CREATE_PARAMS),
  19887.         IS_NULLABLE        = t.allownulls,
  19888.         CASE_SENSITIVE        = d.case_sensitive,
  19889.         SEARCHABLE         = d.searchable,
  19890.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  19891.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  19892.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  19893.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  19894.                         else d.local_type_name
  19895.                         end,
  19896.         MINIMUM_SCALE        = convert(smallint,
  19897.                         case 
  19898.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  19899.                         else null 
  19900.                         end),
  19901.         MAXIMUM_SCALE        = convert(smallint,
  19902.                         case 
  19903.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19904.                         then @@max_precision
  19905.                         else null 
  19906.                         end),
  19907.         GUID            = convert(binary(16),null),
  19908.         TYPELIB            = convert(varchar(1),null),
  19909.         VERSION            = convert(varchar(1),null),
  19910.         IS_LONG            = d.is_long,
  19911.         BEST_MATCH        = case when t.usertype = 80
  19912.                         then convert(bit,0)
  19913.                         else d.best_match
  19914.                         end,
  19915.         IS_FIXEDLENGTH        = convert(bit, 
  19916.                         case when d.fixlen is null then 0 else 1 end)
  19917.     from     master.dbo.spt_provider_types d, master.dbo.spt_datatype_info_ext e, systypes t
  19918.     where    d.ss_dtype = t.type 
  19919.     and    t.usertype <= 100
  19920.     and    t.usertype <> 18 /* sysname */
  19921.     and    (case when t.usertype = 80 /* TIMESTAMP */ then 1 else 0 end 
  19922.             = case when d.type_name = 'timestamp' then 1 else 0 end)
  19923.     and     t.usertype *= e.user_type 
  19924.     and     e.AUTO_INCREMENT = 0
  19925.     and     t.type not in (111,109,38,110,55,63)    /* get rid of nullable types */
  19926.     and    (@data_type is null or d.oledb_data_type = @data_type)
  19927.     and    (@best_match is null or d.best_match = @best_match)
  19928.     order by 2
  19929. go
  19930. dump tran master with no_log
  19931. go
  19932.  
  19933. if (charindex('7.00', @@version) = 0 and
  19934.     charindex('8.00', @@version) = 0)
  19935. begin
  19936.     print ''
  19937.     print ''
  19938.     print 'Warning:'
  19939.     print 'you are installing the stored procedures '
  19940.     print 'on a pre 8.0 SQL Server.'
  19941.     print 'Ignore the following errors.'
  19942. end
  19943. else
  19944.     drop proc sp_provider_types_rowset
  19945. go
  19946.  
  19947. /*    Procedure for 8.0 server */
  19948. create proc sp_provider_types_rowset
  19949.     (
  19950.     @data_type     smallint = null,
  19951.     @best_match    tinyint  = null
  19952.     )
  19953. as
  19954.     select
  19955.         TYPE_NAME         = case    when t.usertype = 80 then t.name 
  19956.                         else d.type_name 
  19957.                         end,
  19958.         DATA_TYPE         = d.oledb_data_type,                            
  19959.  
  19960.  
  19961.         COLUMN_SIZE        = case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19962.                             then @@max_precision
  19963.                             else coalesce(d.column_size,d.data_precision,t.prec) end,
  19964.         LITERAL_PREFIX         = d.literal_prefix,
  19965.         LITERAL_SUFFIX         = d.literal_suffix,
  19966.         CREATE_PARAMS         = convert(nvarchar(32),e.CREATE_PARAMS),
  19967.         IS_NULLABLE        = t.allownulls,
  19968.         CASE_SENSITIVE        = d.case_sensitive,
  19969.         SEARCHABLE         = d.searchable,
  19970.         UNSIGNED_ATTRIBUTE    = d.unsigned_attribute,
  19971.         FIXED_PREC_SCALE    = d.fixed_prec_scale,
  19972.         AUTO_UNIQUE_VALUE    = d.auto_unique_value, 
  19973.         LOCAL_TYPE_NAME        = case    when t.usertype = 80 then t.name 
  19974.                         else d.local_type_name
  19975.                         end,
  19976.         MINIMUM_SCALE        = convert(smallint,
  19977.                         case 
  19978.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then 0 
  19979.                         else null 
  19980.                         end),
  19981.         MAXIMUM_SCALE        = convert(smallint,
  19982.                         case 
  19983.                         when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ 
  19984.                         then @@max_precision
  19985.                         else null 
  19986.                         end),
  19987.         GUID            = convert(uniqueidentifier,null),
  19988.         TYPELIB            = convert(nvarchar(1),null),
  19989.         VERSION            = convert(nvarchar(1),null),
  19990.         IS_LONG            = d.is_long,
  19991.         BEST_MATCH        = case    when t.usertype = 80
  19992.                         then convert(bit,0)
  19993.                         else d.best_match
  19994.                         end,
  19995.         IS_FIXEDLENGTH        = convert(bit, 
  19996.                         case when d.fixlen is null then 0 else 1 end)
  19997.  
  19998.     from master.dbo.spt_provider_types d
  19999.         INNER JOIN master.dbo.systypes t on d.ss_dtype = t.xtype
  20000.         LEFT OUTER JOIN master.dbo.spt_datatype_info_ext e on
  20001.             t.xusertype = e.user_type
  20002.             and e.AUTO_INCREMENT = 0
  20003.     where    (@data_type is null or d.oledb_data_type = @data_type)
  20004.     and    (@best_match is null or d.best_match = @best_match)    
  20005.     and    t.usertype <= 255
  20006.     and    t.usertype <> 18 /* sysname */
  20007.     order by 2
  20008. go
  20009.  
  20010. grant execute on sp_provider_types_rowset to public
  20011. go
  20012.  
  20013. dump tran master with no_log
  20014. go
  20015. if (charindex('6.00', @@version) > 0)
  20016.     begin
  20017.     if (exists (select * from sysobjects
  20018.         where name = 'sp_provider_types_rowset' and type = 'P '))
  20019.         begin
  20020.         drop procedure sp_provider_types_rowset
  20021.         dump tran master with no_log
  20022.         end
  20023.     end
  20024. go
  20025.  
  20026.  
  20027. print ''
  20028. print 'creating sp_procedure_params_rowset'
  20029. go
  20030.  
  20031. /*    Procedure for 6.0 and 6.50 servers */
  20032. create procedure sp_procedure_params_rowset
  20033.     (
  20034.        @procedure_name        varchar(255) = null,
  20035.     @group_number        int = null,
  20036.     @procedure_schema     varchar(255) = null,
  20037.     @parameter_name        varchar(255) = null
  20038.     )
  20039. as
  20040. IF @procedure_name is not null
  20041.     BEGIN
  20042.     select
  20043.         PROCEDURE_CATALOG     = db_name(),
  20044.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20045.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20046.         PARAMETER_NAME         = c.name,
  20047.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20048.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  20049.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20050.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20051.         IS_NULLABLE        = convert(bit,
  20052.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20053.                         then 0 else 1
  20054.                         end),
  20055.         DATA_TYPE        = d.oledb_data_type,
  20056.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20057.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20058.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20059.                         then coalesce(d.column_size,c.length)
  20060.                         else null 
  20061.                         end),
  20062.         CHARACTER_OCTET_LENGTH    = convert(int,
  20063.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20064.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20065.                         then coalesce(d.column_size,c.length)
  20066.                         else null 
  20067.                         end),
  20068.         NUMERIC_PRECISION    = convert(smallint,
  20069.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec 
  20070.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20071.                             then d.data_precision else null end),
  20072.         NUMERIC_SCALE        = convert(smallint, 
  20073.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20074.         DESCRIPTION        = convert(varchar(1),null),
  20075.         TYPE_NAME        = d.type_name,
  20076.         LOCAL_TYPE_NAME        = d.local_type_name
  20077.     
  20078.     from
  20079.         syscolumns c,
  20080.         sysobjects o,
  20081.         master.dbo.spt_provider_types d,
  20082.         systypes t
  20083.     where
  20084.         o.name = @procedure_name
  20085.     and    o.type = 'P'                            /* Just Procedures */
  20086.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20087.     and    o.id = c.id
  20088.     and    c.number = @group_number
  20089.     and    c.type = d.ss_dtype
  20090.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20091.     and    c.usertype = t.usertype
  20092.     and    (t.usertype != 80 or d.type_name='timestamp')
  20093.     and    (@parameter_name is null or @parameter_name = c.name)
  20094.     UNION ALL
  20095.     SELECT           /* return value row*/
  20096.         PROCEDURE_CATALOG     = db_name(),
  20097.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20098.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20099.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20100.         ORDINAL_POSITION     = convert(smallint,0),
  20101.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20102.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20103.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20104.         IS_NULLABLE        = convert(bit,0),
  20105.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20106.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20107.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20108.         NUMERIC_PRECISION    = convert(smallint,10),
  20109.         NUMERIC_SCALE        = convert(smallint,null),
  20110.         DESCRIPTION        = convert(varchar(1),null),
  20111.         TYPE_NAME        = convert(sysname,'int'),
  20112.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20113.     from
  20114.         syscomments c,
  20115.         sysobjects o
  20116.     where
  20117.         o.name = @procedure_name
  20118.     and    o.type = 'P'                        /* Just Procedures */
  20119.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20120.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20121.     and    c.id = o.id
  20122.     and    c.number = @group_number
  20123.     and c.colid = 1
  20124.     order by 2, 3, 5
  20125.     END
  20126. ELSE
  20127.     BEGIN
  20128.     select
  20129.         PROCEDURE_CATALOG     = db_name(),
  20130.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20131.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20132.         PARAMETER_NAME         = c.name,
  20133.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20134.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  20135.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20136.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20137.         IS_NULLABLE        = convert(bit,
  20138.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20139.                         then 0 else 1
  20140.                         end),
  20141.         DATA_TYPE        = d.oledb_data_type,
  20142.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20143.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20144.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20145.                         then coalesce(d.column_size,c.length)
  20146.                         else null 
  20147.                         end),
  20148.         CHARACTER_OCTET_LENGTH    = convert(int,
  20149.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20150.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20151.                         then coalesce(d.column_size,c.length)
  20152.                         else null 
  20153.                         end),
  20154.         NUMERIC_PRECISION    = convert(smallint,
  20155.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20156.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20157.                             then d.data_precision else null end),
  20158.         NUMERIC_SCALE        = convert(smallint, 
  20159.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20160.         DESCRIPTION        = convert(varchar(1),null),
  20161.         TYPE_NAME        = d.type_name,
  20162.         LOCAL_TYPE_NAME        = d.local_type_name
  20163.     
  20164.     from
  20165.         syscolumns c,
  20166.         sysobjects o,
  20167.         master.dbo.spt_provider_types d,
  20168.         systypes t
  20169.     where
  20170.         o.type = 'P'                            /* Just Procedures */
  20171.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20172.     and    o.id = c.id
  20173.     and    c.type = d.ss_dtype
  20174.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20175.     and    c.usertype = t.usertype
  20176.     and    (t.usertype != 80 or d.type_name='timestamp')
  20177.     and    (@parameter_name is null or @parameter_name = c.name)
  20178.     UNION ALL
  20179.     SELECT           /* return value row*/
  20180.         PROCEDURE_CATALOG     = db_name(),
  20181.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20182.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20183.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20184.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20185.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20186.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20187.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20188.         IS_NULLABLE        = convert(bit,0),
  20189.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20190.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20191.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20192.         NUMERIC_PRECISION    = convert(smallint,10),
  20193.         NUMERIC_SCALE        = convert(smallint,null),
  20194.         DESCRIPTION        = convert(varchar(1),null),
  20195.         TYPE_NAME        = convert(sysname,'int'),
  20196.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20197.     from
  20198.         syscomments c,
  20199.         sysobjects o
  20200.     where
  20201.         o.type = 'P'                        /* Just Procedures */
  20202.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20203.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20204.     and    c.id = o.id
  20205.     and     c.colid = 1
  20206.     order by 2, 3, 5
  20207.     END
  20208. go
  20209. dump tran master with no_log
  20210. go
  20211. create procedure sp_procedure_params_rowset;2
  20212.     (
  20213.       @handle            int output,
  20214.        @scrollopt        int output,
  20215.     @ccopt            int output,
  20216.     @rows            int output,
  20217.        @procedure_name        varchar(255) = null,
  20218.     @group_number        int = null,
  20219.     @procedure_schema     varchar(255) = null,
  20220.     @parameter_name        varchar(255) = null
  20221.     )
  20222. as
  20223.     declare @ret int
  20224.  
  20225.  
  20226. SET NOCOUNT ON
  20227.  
  20228.     create table #spprocparamrowset1
  20229.         (
  20230.         PROCEDURE_CATALOG     sysname not null,
  20231.         PROCEDURE_SCHEMA     sysname not null,
  20232.         PROCEDURE_NAME         varchar(35) not null,
  20233.         PARAMETER_NAME         sysname not null,
  20234.         ORDINAL_POSITION     smallint not null,
  20235.         PARAMETER_TYPE         smallint null,
  20236.         PARAMETER_HASDEFAULT    tinyint null,
  20237.         PARAMETER_DEFAULT    varchar(255) null,
  20238.         IS_NULLABLE        bit not null,
  20239.         DATA_TYPE        smallint null,
  20240.         CHARACTER_MAXIMUM_LENGTH int null,
  20241.         CHARACTER_OCTET_LENGTH    int null,
  20242.         NUMERIC_PRECISION    smallint null,
  20243.         NUMERIC_SCALE        smallint null,
  20244.         DESCRIPTION        varchar(1) null,
  20245.         TYPE_NAME        sysname null,
  20246.         LOCAL_TYPE_NAME        sysname null,
  20247.         )
  20248.  
  20249. IF @procedure_name is not null
  20250.     BEGIN
  20251.     insert into #spprocparamrowset1
  20252.     select
  20253.         PROCEDURE_CATALOG     = db_name(),
  20254.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20255.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20256.         PARAMETER_NAME         = c.name,
  20257.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20258.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  20259.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20260.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20261.         IS_NULLABLE        = convert(bit,
  20262.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20263.                         then 0 else 1
  20264.                         end),
  20265.         DATA_TYPE        = d.oledb_data_type,
  20266.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20267.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20268.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20269.                         then coalesce(d.column_size,c.length)
  20270.                         else null 
  20271.                         end),
  20272.         CHARACTER_OCTET_LENGTH    = convert(int,
  20273.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20274.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20275.                         then coalesce(d.column_size,c.length)
  20276.                         else null 
  20277.                         end),
  20278.         NUMERIC_PRECISION    = convert(smallint,
  20279.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20280.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20281.                             then d.data_precision else null end),
  20282.         NUMERIC_SCALE        = convert(smallint, 
  20283.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20284.         DESCRIPTION        = convert(varchar(1),null),
  20285.         TYPE_NAME        = d.type_name,
  20286.         LOCAL_TYPE_NAME        = d.local_type_name
  20287.     
  20288.     from
  20289.         syscolumns c,
  20290.         sysobjects o,
  20291.         master.dbo.spt_provider_types d,
  20292.         systypes t
  20293.     where
  20294.         o.name = @procedure_name
  20295.     and    o.type = 'P'                            /* Just Procedures */
  20296.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20297.     and    o.id = c.id
  20298.     and    c.number = @group_number
  20299.     and    c.type = d.ss_dtype
  20300.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20301.     and    c.usertype = t.usertype
  20302.     and    (t.usertype != 80 or d.type_name='timestamp')
  20303.     and    (@parameter_name is null or @parameter_name = c.name)
  20304.     UNION ALL
  20305.     SELECT           /* return value row*/
  20306.         PROCEDURE_CATALOG     = db_name(),
  20307.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20308.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20309.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20310.         ORDINAL_POSITION     = convert(smallint,0),
  20311.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20312.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20313.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20314.         IS_NULLABLE        = convert(bit,0),
  20315.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20316.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20317.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20318.         NUMERIC_PRECISION    = convert(smallint,10),
  20319.         NUMERIC_SCALE        = convert(smallint,null),
  20320.         DESCRIPTION        = convert(varchar(1),null),
  20321.         TYPE_NAME        = convert(sysname,'int'),
  20322.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20323.     from
  20324.         syscomments c,
  20325.         sysobjects o
  20326.     where
  20327.         o.name = @procedure_name
  20328.     and    o.type = 'P'                        /* Just Procedures */
  20329.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20330.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20331.     and    c.id = o.id
  20332.     and    c.number = @group_number
  20333.     and c.colid = 1
  20334.     order by 2, 3, 5
  20335.     END
  20336. ELSE
  20337.     BEGIN
  20338.     insert into #spprocparamrowset1
  20339.     select
  20340.         PROCEDURE_CATALOG     = db_name(),
  20341.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20342.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20343.         PARAMETER_NAME         = c.name,
  20344.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20345.         PARAMETER_TYPE         = convert(smallint, 1+((c.status/64)&1)),
  20346.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20347.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20348.         IS_NULLABLE        = convert(bit,
  20349.                         case when d.oledb_data_type = 11 /*DBTYPE_BOOL*/ 
  20350.                         then 0 else 1
  20351.                         end),
  20352.         DATA_TYPE        = d.oledb_data_type,
  20353.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20354.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20355.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20356.                         then coalesce(d.column_size,c.length)
  20357.                         else null 
  20358.                         end),
  20359.         CHARACTER_OCTET_LENGTH    = convert(int,
  20360.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20361.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20362.                         then coalesce(d.column_size,c.length)
  20363.                         else null 
  20364.                         end),
  20365.         NUMERIC_PRECISION    = convert(smallint,
  20366.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20367.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20368.                             then d.data_precision else null end),
  20369.         NUMERIC_SCALE        = convert(smallint, 
  20370.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20371.         DESCRIPTION        = convert(varchar(1),null),
  20372.         TYPE_NAME        = d.type_name,
  20373.         LOCAL_TYPE_NAME        = d.local_type_name
  20374.     
  20375.     from
  20376.         syscolumns c,
  20377.         sysobjects o,
  20378.         master.dbo.spt_provider_types d,
  20379.         systypes t
  20380.     where
  20381.         o.type = 'P'                            /* Just Procedures */
  20382.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20383.     and    o.id = c.id
  20384.     and    c.type = d.ss_dtype
  20385.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20386.     and    c.usertype = t.usertype
  20387.     and    (t.usertype != 80 or d.type_name='timestamp')
  20388.     and    (@parameter_name is null or @parameter_name = c.name)
  20389.     UNION ALL
  20390.     SELECT           /* return value row*/
  20391.         PROCEDURE_CATALOG     = db_name(),
  20392.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20393.         PROCEDURE_NAME         = convert(varchar(35),o.name +';'+ ltrim(str(c.number,5))),
  20394.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20395.         ORDINAL_POSITION     = convert(smallint,0),
  20396.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20397.         PARAMETER_HASDEFAULT    = convert(tinyint,0),
  20398.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20399.         IS_NULLABLE        = convert(bit,0),
  20400.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20401.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20402.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20403.         NUMERIC_PRECISION    = convert(smallint,10),
  20404.         NUMERIC_SCALE        = convert(smallint,null),
  20405.         DESCRIPTION        = convert(varchar(1),null),
  20406.         TYPE_NAME        = convert(sysname,'int'),
  20407.         LOCAL_TYPE_NAME        = convert(sysname,'int')
  20408.     from
  20409.         syscomments c,
  20410.         sysobjects o
  20411.     where
  20412.         o.type = 'P'                        /* Just Procedures */
  20413.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20414.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20415.     and    c.id = o.id
  20416.     and     c.colid = 1
  20417.     order by 2, 3, 5
  20418.     END
  20419.  
  20420.     exec @ret = sp_cursoropen @handle output, 'select * from #spprocparamrowset1',
  20421.         @scrollopt output, @ccopt output, @rows output
  20422.  
  20423.     drop table #spprocparamrowset1
  20424.        return isnull(@ret,0)
  20425. go
  20426. dump tran master with no_log
  20427. go
  20428. create procedure sp_procedure_params_rowset;3
  20429. as
  20430.     select
  20431.         PROCEDURE_CATALOG     = convert(sysname, ' '),
  20432.         PROCEDURE_SCHEMA     = convert(sysname, ' '),
  20433.         PROCEDURE_NAME         = convert(varchar(35), ' '),
  20434.         PARAMETER_NAME         = convert(sysname, ' '),
  20435.         ORDINAL_POSITION     = convert(smallint, 0),
  20436.         PARAMETER_TYPE         = convert(smallint, 0),
  20437.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20438.         PARAMETER_DEFAULT    = convert(varchar(255),null),
  20439.         IS_NULLABLE        = convert(bit, 0),
  20440.         DATA_TYPE        = convert(smallint, 0),
  20441.         CHARACTER_MAXIMUM_LENGTH= convert(int, 0),
  20442.         CHARACTER_OCTET_LENGTH    = convert(int, 0),
  20443.         NUMERIC_PRECISION    = convert(smallint, 0),
  20444.         NUMERIC_SCALE        = convert(smallint, 0),
  20445.         DESCRIPTION        = convert(varchar(1),null),
  20446.         TYPE_NAME        = convert(sysname,null),
  20447.         LOCAL_TYPE_NAME        = convert(sysname,null)
  20448.     where    1=0
  20449. go
  20450. dump tran master with no_log
  20451. go
  20452.  
  20453.  
  20454. if (charindex('7.00', @@version) > 0 or
  20455.     charindex('8.00', @@version) > 0)
  20456.     drop procedure sp_procedure_params_rowset
  20457. else
  20458. begin
  20459.     print ''
  20460.     print ''
  20461.     print 'Warning:'
  20462.     print 'you are installing the stored procedures '
  20463.     print 'on a pre 7.0 SQL Server.'
  20464.     print 'Ignore the following errors.'
  20465. end
  20466. go
  20467.  
  20468. /*    Procedure for 7.0 servers */
  20469. create procedure sp_procedure_params_rowset
  20470.     (
  20471.        @procedure_name        sysname,
  20472.     @group_number        int = 1,
  20473.     @procedure_schema     sysname = null,
  20474.     @parameter_name        sysname = null
  20475.     )
  20476. as
  20477.     select
  20478.         PROCEDURE_CATALOG     = db_name(),
  20479.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20480.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20481.         PARAMETER_NAME         = c.name,
  20482.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20483.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20484.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20485.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20486.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20487.         DATA_TYPE        = d.oledb_data_type,
  20488.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20489.                         case 
  20490.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20491.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20492.                         then coalesce(d.column_size,c.length)
  20493.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20494.                         then coalesce(d.column_size,c.length/2)
  20495.                         else null 
  20496.                         end),
  20497.         CHARACTER_OCTET_LENGTH    = convert(int,
  20498.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20499.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20500.                         then coalesce(d.column_size,c.length)
  20501.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20502.                         then coalesce(d.column_size*2,c.length)
  20503.                         else null 
  20504.                         end),
  20505.         NUMERIC_PRECISION    = convert(smallint,
  20506.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20507.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20508.                             then d.data_precision else null end),
  20509.         NUMERIC_SCALE        = convert(smallint, 
  20510.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20511.         DESCRIPTION        = convert(nvarchar(1),null),
  20512.         TYPE_NAME        = d.type_name,
  20513.         LOCAL_TYPE_NAME        = d.local_type_name
  20514.     
  20515.     from
  20516.         sysobjects o,
  20517.         syscolumns c,
  20518.         master.dbo.spt_provider_types d,
  20519.         systypes t
  20520.     where
  20521.         o.name = @procedure_name
  20522.     and    o.type = 'P'                            /* Just Procedures */
  20523.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20524.     and    o.id = c.id
  20525.     and    c.number = @group_number
  20526.     and    c.xtype = d.ss_dtype
  20527.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20528.     and    c.xusertype = t.xusertype
  20529.     and    (@parameter_name is null or @parameter_name = c.name)
  20530.     UNION ALL
  20531.     SELECT           /* return value row*/
  20532.         PROCEDURE_CATALOG     = db_name(),
  20533.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20534.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20535.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20536.         ORDINAL_POSITION     = convert(smallint,0),
  20537.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20538.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20539.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20540.         IS_NULLABLE        = convert(bit,0),
  20541.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20542.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20543.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20544.         NUMERIC_PRECISION    = convert(smallint,10),
  20545.         NUMERIC_SCALE        = convert(smallint,null),
  20546.         DESCRIPTION        = convert(nvarchar(1),null),
  20547.         TYPE_NAME        = convert(sysname,N'int'),
  20548.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20549.     from
  20550.         sysobjects o,
  20551.         syscomments c
  20552.     where
  20553.         o.name = @procedure_name
  20554.     and    o.id = c.id 
  20555.     and    c.number = @group_number
  20556.     and    c.colid = 1
  20557.     and    o.type = 'P'                        /* Just Procedures */
  20558.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20559.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20560.     order by 2, 3, 5
  20561. go
  20562. dump tran master with no_log
  20563. go
  20564. create procedure sp_procedure_params_rowset;2
  20565.     (
  20566.     @procedure_schema     sysname = null,
  20567.     @parameter_name        sysname = null
  20568.     )
  20569. as
  20570.     select
  20571.         PROCEDURE_CATALOG     = db_name(),
  20572.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20573.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20574.         PARAMETER_NAME         = c.name,
  20575.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20576.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20577.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20578.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20579.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20580.         DATA_TYPE        = d.oledb_data_type,
  20581.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20582.                         case 
  20583.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20584.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20585.                         then coalesce(d.column_size,c.length)
  20586.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20587.                         then coalesce(d.column_size,c.length/2)
  20588.                         else null 
  20589.                         end),
  20590.         CHARACTER_OCTET_LENGTH    = convert(int,
  20591.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20592.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20593.                         then coalesce(d.column_size,c.length)
  20594.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20595.                         then coalesce(d.column_size*2,c.length)
  20596.                         else null 
  20597.                         end),
  20598.         NUMERIC_PRECISION    = convert(smallint,
  20599.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20600.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20601.                             then d.data_precision else null end),
  20602.         NUMERIC_SCALE        = convert(smallint, 
  20603.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20604.         DESCRIPTION        = convert(nvarchar(1),null),
  20605.         TYPE_NAME        = d.type_name,
  20606.         LOCAL_TYPE_NAME        = d.local_type_name
  20607.     
  20608.     from
  20609.         sysobjects o,
  20610.         syscolumns c,
  20611.         master.dbo.spt_provider_types d,
  20612.         systypes t
  20613.     where
  20614.         o.type = 'P'                            /* Just Procedures */
  20615.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20616.     and    o.id = c.id
  20617.     and    c.xtype = d.ss_dtype
  20618.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20619.     and    c.xusertype = t.xusertype
  20620.     and    (@parameter_name is null or @parameter_name = c.name)
  20621.     UNION ALL
  20622.     SELECT           /* return value row*/
  20623.         PROCEDURE_CATALOG     = db_name(),
  20624.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20625.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20626.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20627.         ORDINAL_POSITION     = convert(smallint,0),
  20628.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20629.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20630.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20631.         IS_NULLABLE        = convert(bit,0),
  20632.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20633.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20634.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20635.         NUMERIC_PRECISION    = convert(smallint,10),
  20636.         NUMERIC_SCALE        = convert(smallint,null),
  20637.         DESCRIPTION        = convert(nvarchar(1),null),
  20638.         TYPE_NAME        = convert(sysname,N'int'),
  20639.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20640.     from
  20641.         sysobjects o,
  20642.         syscomments c
  20643.     where
  20644.         o.type = 'P'        /* Just Procedures */
  20645.     and    o.id = c.id 
  20646.     and    c.colid = 1
  20647.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20648.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20649.     order by 2, 3, 5
  20650. go
  20651.  
  20652. if (charindex('8.00', @@version) > 0)
  20653.     drop procedure sp_procedure_params_rowset
  20654. else
  20655. begin
  20656.     print ''
  20657.     print ''
  20658.     print 'Warning:'
  20659.     print 'you are installing the stored procedures '
  20660.     print 'on a pre 8.0 SQL Server.'
  20661.     print 'Ignore the following errors.'
  20662. end
  20663. go
  20664.  
  20665. /*    Procedure for 8.0 servers */
  20666. create procedure sp_procedure_params_rowset
  20667.     (
  20668.        @procedure_name        sysname,
  20669.     @group_number        int = 1,
  20670.     @procedure_schema     sysname = null,
  20671.     @parameter_name        sysname = null
  20672.     )
  20673. as
  20674.     select
  20675.         PROCEDURE_CATALOG     = db_name(),
  20676.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20677.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20678.         PARAMETER_NAME         = c.name,
  20679.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20680.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20681.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20682.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20683.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20684.         DATA_TYPE        = d.oledb_data_type,
  20685.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20686.                         case 
  20687.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20688.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20689.                         then coalesce(d.column_size,c.length)
  20690.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20691.                         then coalesce(d.column_size,c.length/2)
  20692.                         else null 
  20693.                         end),
  20694.         CHARACTER_OCTET_LENGTH    = convert(int,
  20695.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20696.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20697.                         then coalesce(d.column_size,c.length)
  20698.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20699.                         then coalesce(d.column_size*2,c.length)
  20700.                         else null 
  20701.                         end),
  20702.         NUMERIC_PRECISION    = convert(smallint,
  20703.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20704.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20705.                             then d.data_precision else null end),
  20706.         NUMERIC_SCALE        = convert(smallint, 
  20707.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20708.         DESCRIPTION        = convert(nvarchar(1),null),
  20709.         TYPE_NAME        = d.type_name,
  20710.         LOCAL_TYPE_NAME        = d.local_type_name
  20711.     
  20712.     from
  20713.         sysobjects o,
  20714.         syscolumns c,
  20715.         master.dbo.spt_provider_types d,
  20716.         systypes t
  20717.     where
  20718.         o.name = @procedure_name
  20719.     and     (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  20720.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20721.     and    o.id = c.id
  20722.     and    ((c.number = @group_number and o.type = 'P') 
  20723.             or (c.number = 0 and o.type = 'FN')
  20724.             or (c.number = 1 and o.type in ('TF', 'IF')))
  20725.     and    c.xtype = d.ss_dtype
  20726.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20727.     and    c.xusertype = t.xusertype
  20728.     and    (@parameter_name is null or @parameter_name = c.name)
  20729.     UNION ALL
  20730.     SELECT           /* return value row*/
  20731.         PROCEDURE_CATALOG     = db_name(),
  20732.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20733.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20734.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20735.         ORDINAL_POSITION     = convert(smallint,0),
  20736.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20737.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20738.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20739.         IS_NULLABLE        = convert(bit,0),
  20740.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20741.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20742.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20743.         NUMERIC_PRECISION    = convert(smallint,10),
  20744.         NUMERIC_SCALE        = convert(smallint,null),
  20745.         DESCRIPTION        = convert(nvarchar(1),null),
  20746.         TYPE_NAME        = convert(sysname,N'int'),
  20747.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20748.     from
  20749.         sysobjects o,
  20750.         syscomments c
  20751.     where
  20752.         o.name = @procedure_name
  20753.     and    o.id = c.id 
  20754.     and    c.number = @group_number
  20755.     and    c.colid = 1
  20756.     and    o.type = 'P'            /* Just Procedures */
  20757.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20758.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20759.     UNION ALL
  20760.     SELECT           /* UDF return value row*/
  20761.         PROCEDURE_CATALOG          = db_name(),
  20762.         PROCEDURE_SCHEMA          = user_name(o.uid),
  20763.         PROCEDURE_NAME              = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20764.         PARAMETER_NAME              = convert(sysname,'@RETURN_VALUE'),
  20765.         ORDINAL_POSITION          = convert(smallint, 0),
  20766.         PARAMETER_TYPE              = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20767.         PARAMETER_HASDEFAULT     = convert(tinyint, 0),
  20768.         PARAMETER_DEFAULT         = convert(nvarchar(255),null),
  20769.         IS_NULLABLE                 = convert(bit, c.isnullable),
  20770.         DATA_TYPE                 = d.oledb_data_type,
  20771.         CHARACTER_MAXIMUM_LENGTH = convert(int,
  20772.                         case 
  20773.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20774.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20775.                         then coalesce(d.column_size,c.length)
  20776.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20777.                         then coalesce(d.column_size,c.length/2)
  20778.                         else null 
  20779.                         end),
  20780.         CHARACTER_OCTET_LENGTH     = convert(int,
  20781.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20782.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20783.                         then coalesce(d.column_size,c.length)
  20784.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20785.                         then coalesce(d.column_size*2,c.length)
  20786.                         else null 
  20787.                         end),
  20788.         NUMERIC_PRECISION         = convert(smallint,
  20789.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20790.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20791.                             then d.data_precision else null end),
  20792.         NUMERIC_SCALE             = convert(smallint, 
  20793.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20794.         DESCRIPTION                 = convert(nvarchar(1),null),
  20795.         TYPE_NAME                 = d.type_name,
  20796.         LOCAL_TYPE_NAME             = d.local_type_name
  20797.     FROM
  20798.         sysobjects o,
  20799.         syscolumns c,
  20800.         master.dbo.spt_provider_types d,
  20801.         systypes t
  20802.     WHERE
  20803.         o.name = @procedure_name
  20804.     and    o.id = c.id 
  20805.     and    c.number = 0
  20806.     and    c.colid = 0
  20807.     and    o.type = 'FN'            /* UDF scalar functions */
  20808.     and    c.xtype = d.ss_dtype
  20809.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20810.     and    c.xusertype = t.xusertype
  20811.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20812.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20813.     UNION ALL
  20814.     SELECT           /* UDF table value row*/
  20815.         PROCEDURE_CATALOG     = db_name(),
  20816.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20817.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20818.         PARAMETER_NAME         = convert(sysname,'@TABLE_RETURN_VALUE'),
  20819.         ORDINAL_POSITION     = convert(smallint, 0),
  20820.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20821.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20822.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20823.         IS_NULLABLE        = convert(bit,0),
  20824.         DATA_TYPE        = convert(smallint, 0),                /*DBTYPE_EMPTY*/
  20825.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20826.                         case 
  20827.                         when d.oledb_data_type = 129         /*DBTYPE_STR*/ 
  20828.                             or d.oledb_data_type = 128    /*DBTYPE_BYTES*/
  20829.                         then coalesce(d.column_size,c.length)
  20830.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  20831.                         then coalesce(d.column_size,c.length/2)
  20832.                         else null 
  20833.                         end),
  20834.         CHARACTER_OCTET_LENGTH    = convert(int,
  20835.                         case when d.oledb_data_type = 129    /*DBTYPE_STR*/ 
  20836.                             or d.oledb_data_type = 128    /*DBTYPE_BYTES*/
  20837.                         then coalesce(d.column_size,c.length)
  20838.                         when d.oledb_data_type = 130        /*DBTYPE_WSTR*/
  20839.                         then coalesce(d.column_size*2,c.length)
  20840.                         else null 
  20841.                         end),
  20842.         NUMERIC_PRECISION    = convert(smallint,
  20843.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20844.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20845.                             then d.data_precision else null end),
  20846.         NUMERIC_SCALE        = convert(smallint, 
  20847.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20848.         DESCRIPTION        = convert(nvarchar(50), N'Result table returned by table valued function'),
  20849.         TYPE_NAME        = N'table',
  20850.         LOCAL_TYPE_NAME        = N'table'
  20851.     
  20852.     from
  20853.         sysobjects o,
  20854.         syscolumns c,
  20855.         master.dbo.spt_provider_types d
  20856.     where
  20857.         o.name = @procedure_name
  20858.     and    o.id = c.id 
  20859.     and    c.number = 0
  20860.     and    c.colid = 1
  20861.     and    o.type in ('TF', 'IF')            /* UDF table functions */
  20862.     and    c.xtype = d.ss_dtype
  20863.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20864.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20865.     and    (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  20866.     order by 2, 3, 5
  20867. go
  20868. dump tran master with no_log
  20869. go
  20870. create procedure sp_procedure_params_rowset;2
  20871.     (
  20872.     @procedure_schema     sysname = null,
  20873.     @parameter_name        sysname = null
  20874.     )
  20875. as
  20876.     select
  20877.         PROCEDURE_CATALOG     = db_name(),
  20878.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20879.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20880.         PARAMETER_NAME         = c.name,
  20881.         ORDINAL_POSITION     = convert(smallint, c.colid),
  20882.         PARAMETER_TYPE         = convert(smallint, 1+c.isoutparam),
  20883.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20884.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20885.         IS_NULLABLE        = convert(bit,ColumnProperty(c.id,c.name,'AllowsNull')),
  20886.         DATA_TYPE        = d.oledb_data_type,
  20887.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20888.                         case 
  20889.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20890.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20891.                         then coalesce(d.column_size,c.length)
  20892.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20893.                         then coalesce(d.column_size,c.length/2)
  20894.                         else null 
  20895.                         end),
  20896.         CHARACTER_OCTET_LENGTH    = convert(int,
  20897.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20898.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20899.                         then coalesce(d.column_size,c.length)
  20900.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20901.                         then coalesce(d.column_size*2,c.length)
  20902.                         else null 
  20903.                         end),
  20904.         NUMERIC_PRECISION    = convert(smallint,
  20905.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20906.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20907.                             then d.data_precision else null end),
  20908.         NUMERIC_SCALE        = convert(smallint, 
  20909.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20910.         DESCRIPTION        = convert(nvarchar(1),null),
  20911.         TYPE_NAME        = d.type_name,
  20912.         LOCAL_TYPE_NAME        = d.local_type_name
  20913.     
  20914.     from
  20915.         sysobjects o,
  20916.         syscolumns c,
  20917.         master.dbo.spt_provider_types d,
  20918.         systypes t
  20919.     where
  20920.         (o.type in ('P', 'TF', 'IF') OR (len(c.name) > 0 and o.type = 'FN'))
  20921.     and    (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20922.     and    o.id = c.id
  20923.     and    (o.type = 'P' or (c.number = 0 and o.type = 'FN') or (c.number = 1 and o.type in ('TF', 'IF')))
  20924.     and    c.xtype = d.ss_dtype
  20925.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  20926.     and    c.xusertype = t.xusertype
  20927.     and    (@parameter_name is null or @parameter_name = c.name)
  20928.     UNION ALL
  20929.     SELECT           /* return value row*/
  20930.         PROCEDURE_CATALOG     = db_name(),
  20931.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20932.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20933.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20934.         ORDINAL_POSITION     = convert(smallint,0),
  20935.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20936.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20937.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20938.         IS_NULLABLE        = convert(bit,0),
  20939.         DATA_TYPE        = convert(smallint, 3 /*DBTYPE_I4*/),
  20940.         CHARACTER_MAXIMUM_LENGTH= convert(int,null),
  20941.         CHARACTER_OCTET_LENGTH    = convert(int,null),
  20942.         NUMERIC_PRECISION    = convert(smallint,10),
  20943.         NUMERIC_SCALE        = convert(smallint,null),
  20944.         DESCRIPTION        = convert(nvarchar(1),null),
  20945.         TYPE_NAME        = convert(sysname,N'int'),
  20946.         LOCAL_TYPE_NAME        = convert(sysname,N'int')
  20947.     from
  20948.         sysobjects o,
  20949.         syscomments c
  20950.     where
  20951.         o.type = 'P'            /* Just Procedures */
  20952.     and    o.id = c.id 
  20953.     and    c.colid = 1
  20954.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  20955.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  20956.     UNION ALL
  20957.     SELECT           /* UDF return value row*/
  20958.         PROCEDURE_CATALOG     = db_name(),
  20959.         PROCEDURE_SCHEMA     = user_name(o.uid),
  20960.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  20961.         PARAMETER_NAME         = convert(sysname,'@RETURN_VALUE'),
  20962.         ORDINAL_POSITION     = convert(smallint, 0),
  20963.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  20964.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  20965.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  20966.         IS_NULLABLE        = convert(bit, c.isnullable),
  20967.         DATA_TYPE        = d.oledb_data_type,
  20968.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  20969.                         case 
  20970.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20971.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20972.                         then coalesce(d.column_size,c.length)
  20973.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20974.                         then coalesce(d.column_size,c.length/2)
  20975.                         else null 
  20976.                         end),
  20977.         CHARACTER_OCTET_LENGTH    = convert(int,
  20978.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  20979.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  20980.                         then coalesce(d.column_size,c.length)
  20981.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  20982.                         then coalesce(d.column_size*2,c.length)
  20983.                         else null 
  20984.                         end),
  20985.         NUMERIC_PRECISION    = convert(smallint,
  20986.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  20987.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  20988.                             then d.data_precision else null end),
  20989.         NUMERIC_SCALE        = convert(smallint, 
  20990.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  20991.         DESCRIPTION        = convert(nvarchar(1),null),
  20992.         TYPE_NAME        = d.type_name,
  20993.         LOCAL_TYPE_NAME        = d.local_type_name
  20994.     
  20995.     from
  20996.         sysobjects o,
  20997.         syscolumns c,
  20998.         master.dbo.spt_provider_types d,
  20999.         systypes t
  21000.     where
  21001.         o.id = c.id 
  21002.     and    c.number = 0
  21003.     and    c.colid = 0
  21004.     and    o.type = 'FN'            /* UDF scalar functions */
  21005.     and    c.xtype = d.ss_dtype
  21006.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21007.     and    c.xusertype = t.xusertype
  21008.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21009.     and    (@parameter_name is null or @parameter_name = '@RETURN_VALUE')
  21010.     UNION ALL
  21011.     SELECT           /* UDF table value row*/
  21012.         PROCEDURE_CATALOG     = db_name(),
  21013.         PROCEDURE_SCHEMA     = user_name(o.uid),
  21014.         PROCEDURE_NAME         = convert(nvarchar(134),o.name +';'+ ltrim(str(c.number,5))),
  21015.         PARAMETER_NAME         = convert(sysname,'@TABLE_RETURN_VALUE'),
  21016.         ORDINAL_POSITION     = convert(smallint, 0),
  21017.         PARAMETER_TYPE         = convert(smallint, 4 /*DBPARAMTYPE_RETURNVALUE*/),
  21018.         PARAMETER_HASDEFAULT    = convert(tinyint, 0),
  21019.         PARAMETER_DEFAULT    = convert(nvarchar(255),null),
  21020.         IS_NULLABLE        = convert(bit,0),
  21021.         DATA_TYPE        = convert(smallint, 0),            /*DBTYPE_EMPTY*/
  21022.         CHARACTER_MAXIMUM_LENGTH= convert(int,
  21023.                         case 
  21024.                         when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21025.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21026.                         then coalesce(d.column_size,c.length)
  21027.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21028.                         then coalesce(d.column_size,c.length/2)
  21029.                         else null 
  21030.                         end),
  21031.         CHARACTER_OCTET_LENGTH    = convert(int,
  21032.                         case when d.oledb_data_type = 129 /*DBTYPE_STR*/ 
  21033.                             or d.oledb_data_type = 128 /*DBTYPE_BYTES*/
  21034.                         then coalesce(d.column_size,c.length)
  21035.                         when d.oledb_data_type = 130 /*DBTYPE_WSTR*/
  21036.                         then coalesce(d.column_size*2,c.length)
  21037.                         else null 
  21038.                         end),
  21039.         NUMERIC_PRECISION    = convert(smallint,
  21040.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.prec
  21041.                             when (d.fixed_prec_scale =1  or d.oledb_data_type =5 or d.oledb_data_type =4)
  21042.                             then d.data_precision else null end),
  21043.         NUMERIC_SCALE        = convert(smallint, 
  21044.                         case when d.oledb_data_type = 131 /*DBTYPE_NUMERIC*/ then c.scale else null end),
  21045.         DESCRIPTION        = convert(nvarchar(1),null),
  21046.         TYPE_NAME        = N'table',
  21047.         LOCAL_TYPE_NAME        = N'table'
  21048.     
  21049.     from
  21050.         sysobjects o,
  21051.         syscolumns c,
  21052.         master.dbo.spt_provider_types d
  21053.     where
  21054.         o.id = c.id 
  21055.     and    c.number = 0
  21056.     and    c.colid = 1
  21057.     and    o.type in ('TF', 'IF')            /* UDF table functions */
  21058.     and    c.xtype = d.ss_dtype
  21059.     and    c.length = case when d.fixlen > 0 then d.fixlen else c.length end
  21060.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21061.     and    (@parameter_name is null or @parameter_name = '@TABLE_RETURN_VALUE')
  21062.     order by 2, 3, 5
  21063. go
  21064.  
  21065. grant execute on sp_procedure_params_rowset to public
  21066. go
  21067.  
  21068. dump tran master with no_log
  21069. go
  21070. if (charindex('6.00', @@version) > 0)
  21071.     begin
  21072.     if (exists (select * from sysobjects
  21073.             where name = 'sp_procedure_params_rowset' and type = 'P '))
  21074.         begin
  21075.         drop procedure sp_procedure_params_rowset
  21076.         dump tran master with no_log
  21077.         end
  21078.     end
  21079. go
  21080.  
  21081.  
  21082. print ''
  21083. print 'creating sp_procedures_rowset'
  21084. go
  21085.  
  21086.  
  21087. /* pre 7.0 version */
  21088. create procedure sp_procedures_rowset
  21089.     (
  21090.     @procedure_name        varchar(255), 
  21091.     @group_number        int = 1,
  21092.     @procedure_schema    varchar(255) = null
  21093.     )        
  21094. as
  21095.     select
  21096.         PROCEDURE_CATALOG    = db_name(),
  21097.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21098.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  21099.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21100.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  21101.         DESCRIPTION        = convert(varchar(1),null),
  21102.         DATE_CREATED        = o.crdate,
  21103.         DATE_MODIFIED        = convert(datetime,null)
  21104.     from     
  21105.         sysobjects o, 
  21106.         syscomments p,
  21107.         sysusers u
  21108.     where
  21109.             o.name = @procedure_name
  21110.         and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21111.     and     o.type = 'P'        /* Object type of Procedure */
  21112.     and     p.colid = 1
  21113.     and     p.id = o.id
  21114.     and    p.number = @group_number
  21115.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  21116.     and     (    suser_id() = 1     /* User is the System Administrator */
  21117.         or     o.uid = user_id()    /* User created the object */
  21118.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21119.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21120.              from sysprotects p
  21121.              where p.id = o.id
  21122.                  /*  get rows for public,current user,user's group */
  21123.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21124.                  /* check for SELECT,EXECUTE privilege */
  21125.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  21126.             ) = 1     /* final magic...compare Grants    */
  21127.         )
  21128.     order by 2, 3
  21129. go
  21130. dump tran master with no_log
  21131. go
  21132. create procedure sp_procedures_rowset;2
  21133.     (
  21134.     @procedure_schema    varchar(255) = null
  21135.     )        
  21136. as
  21137.     select
  21138.         PROCEDURE_CATALOG    = db_name(),
  21139.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21140.         PROCEDURE_NAME        = convert(varchar(35),o.name +';'+ ltrim(str(p.number,5))),
  21141.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21142.         PROCEDURE_DEFINITION    = convert(varchar(1),null),
  21143.         DESCRIPTION        = convert(varchar(1),null),
  21144.         DATE_CREATED        = o.crdate,
  21145.         DATE_MODIFIED        = convert(datetime,null)
  21146.     from     
  21147.         sysobjects o, 
  21148.         syscomments p,
  21149.         sysusers u
  21150.     where
  21151.             (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21152.     and     o.type = 'P'        /* Object type of Procedure */
  21153.     and     p.colid = 1
  21154.     and     p.id = o.id
  21155.     and     u.uid = user_id()    /* constrain sysusers uid for use in subquery */
  21156.     and     (    suser_id() = 1     /* User is the System Administrator */
  21157.         or     o.uid = user_id()    /* User created the object */
  21158.             /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21159.         or     ((select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21160.              from sysprotects p
  21161.              where p.id = o.id
  21162.                  /*  get rows for public,current user,user's group */
  21163.                  and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21164.                  /* check for SELECT,EXECUTE privilege */
  21165.              and (action in (193,224)))&1    /* more magic...normalize GRANT */
  21166.             ) = 1     /* final magic...compare Grants    */
  21167.         )
  21168.     order by 2, 3
  21169. go
  21170. dump tran master with no_log
  21171. go
  21172.  
  21173. if (charindex('7.00', @@version) > 0 or
  21174.     charindex('8.00', @@version) > 0)
  21175.     drop procedure sp_procedures_rowset
  21176. else
  21177. begin
  21178.     print ''
  21179.     print ''
  21180.     print 'Warning:'
  21181.     print 'you are installing the stored procedures '
  21182.     print 'on a pre 7.0 SQL Server.'
  21183.     print 'Ignore the following errors.'
  21184. end
  21185. go
  21186.  
  21187. /* 7.0 version */
  21188. create procedure sp_procedures_rowset
  21189.     (
  21190.     @procedure_name        sysname, 
  21191.     @group_number        int = 1,
  21192.     @procedure_schema    sysname = null
  21193.     )        
  21194. as
  21195.     select
  21196.         PROCEDURE_CATALOG    = db_name(),
  21197.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21198.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21199.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21200.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21201.         DESCRIPTION        = convert(nvarchar(1),null),
  21202.         DATE_CREATED        = o.crdate,
  21203.         DATE_MODIFIED        = convert(datetime,null)
  21204.     from     
  21205.         sysobjects o, 
  21206.         syscomments p
  21207.     where
  21208.         permissions(o.id) <> 0
  21209.     and    o.name = @procedure_name
  21210.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21211.     and     o.type = 'P'        /* Object type of Procedure */
  21212.     and     p.colid = 1
  21213.     and     p.id = o.id
  21214.     and    p.number = @group_number
  21215.     order by 2, 3
  21216. go
  21217. dump tran master with no_log
  21218. go
  21219. create procedure sp_procedures_rowset;2
  21220.     (
  21221.     @procedure_schema    sysname = null
  21222.     )        
  21223. as
  21224.     select
  21225.         PROCEDURE_CATALOG    = db_name(),
  21226.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21227.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21228.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21229.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21230.         DESCRIPTION        = convert(nvarchar(1),null),
  21231.         DATE_CREATED        = o.crdate,
  21232.         DATE_MODIFIED        = convert(datetime,null)
  21233.     from     sysobjects o, syscomments p
  21234.     where
  21235.         permissions(o.id) <> 0
  21236.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21237.     and     o.type = 'P'        /* Object type of Procedure */
  21238.     and p.colid = 1
  21239.     and p.id = o.id
  21240.     order by 2, 3
  21241. go
  21242.  
  21243. if (charindex('8.00', @@version) > 0)
  21244.     drop procedure sp_procedures_rowset
  21245. else
  21246. begin
  21247.     print ''
  21248.     print ''
  21249.     print 'Warning:'
  21250.     print 'you are installing the stored procedures '
  21251.     print 'on a pre 8.0 SQL Server.'
  21252.     print 'Ignore the following errors.'
  21253. end
  21254. go
  21255.  
  21256. /* 8.0 version */
  21257. create procedure sp_procedures_rowset
  21258.     (
  21259.     @procedure_name        sysname, 
  21260.     @group_number        int = 1,
  21261.     @procedure_schema    sysname = null
  21262.     )        
  21263. as
  21264.     select
  21265.         PROCEDURE_CATALOG    = db_name(),
  21266.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21267.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21268.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21269.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21270.         DESCRIPTION        = convert(nvarchar(1),null),
  21271.         DATE_CREATED        = o.crdate,
  21272.         DATE_MODIFIED        = convert(datetime,null)
  21273.     from     
  21274.         sysobjects o, 
  21275.         syscomments p
  21276.     where
  21277.         permissions(o.id) <> 0
  21278.     and    o.name = @procedure_name
  21279.     and     (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21280.     and     o.type in ('P', 'FN', 'TF', 'IF')    /* Object type of Procedure */
  21281.     and     p.colid = 1
  21282.     and     p.id = o.id
  21283.     and    p.number = @group_number
  21284.     order by 2, 3
  21285. go
  21286. dump tran master with no_log
  21287. go
  21288. create procedure sp_procedures_rowset;2
  21289.     (
  21290.     @procedure_schema    sysname = null
  21291.     )        
  21292. as
  21293.     select
  21294.         PROCEDURE_CATALOG    = db_name(),
  21295.         PROCEDURE_SCHEMA    = user_name(o.uid),
  21296.         PROCEDURE_NAME        = convert(nvarchar(134),o.name +';'+ ltrim(str(p.number,5))),
  21297.         PROCEDURE_TYPE        = convert(smallint, 3 /*DB_PT_FUNCTION*/),
  21298.         PROCEDURE_DEFINITION    = convert(nvarchar(1),null),
  21299.         DESCRIPTION        = convert(nvarchar(1),null),
  21300.         DATE_CREATED        = o.crdate,
  21301.         DATE_MODIFIED        = convert(datetime,null)
  21302.     from     sysobjects o, syscomments p
  21303.     where
  21304.         permissions(o.id) <> 0
  21305.     and        (@procedure_schema is null or @procedure_schema = user_name(o.uid))
  21306.     and     o.type in ('P', 'FN', 'TF', 'IF')    /* Object type of Procedure */
  21307.     and p.colid = 1
  21308.     and p.id = o.id
  21309.     order by 2, 3
  21310. go
  21311.  
  21312. grant execute on sp_procedures_rowset to public
  21313. go
  21314.  
  21315. dump tran master with no_log
  21316. go
  21317.  
  21318. if (charindex('6.00', @@version) > 0)
  21319.     begin
  21320.     if (exists (select * from sysobjects
  21321.             where name = 'sp_procedures_rowset' and type = 'P '))
  21322.         begin
  21323.         drop procedure sp_procedures_rowset
  21324.         dump tran master with no_log
  21325.         end
  21326.     end
  21327. go
  21328.  
  21329.  
  21330. print ''
  21331. print 'creating sp_schemata_rowset'
  21332. go
  21333.  
  21334. /*    6.0 and 6.5 and 7.0 version */
  21335. create procedure sp_schemata_rowset
  21336.     (
  21337.     @schema_name    varchar(90) = null, 
  21338.     @schema_owner    varchar(90) = null
  21339.     )        
  21340. as
  21341.     select    distinct
  21342.         CATALOG_NAME            = db_name(),
  21343.         SCHEMA_NAME            = user_name(o.uid),    
  21344.         SCHEMA_OWNER            = user_name(o.uid),    
  21345.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,'master'),
  21346.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,'dbo'),
  21347.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  21348.     from    
  21349.         sysobjects o,
  21350.         master.dbo.sysconfigures    cfg,
  21351.         master.dbo.syscharsets        a_cha, /* charset/1001, not sortorder */
  21352.         master.dbo.syscharsets        b_cha  /* sortorder/2001, not charset */
  21353.     where   
  21354.         (@schema_name is null or @schema_name = user_name(o.uid))
  21355.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21356.     and    a_cha.type = 1001 /* type is charset */
  21357.     and     b_cha.type = 2001 /* type is sortorder */
  21358.     and     a_cha.id = b_cha.csid
  21359.     and     b_cha.id = cfg.value
  21360.     order by 2
  21361. go
  21362. dump tran master with no_log
  21363. go
  21364.  
  21365. if (charindex('8.00', @@version) > 0)
  21366.     drop procedure sp_schemata_rowset
  21367. else
  21368. begin
  21369.     print ''
  21370.     print ''
  21371.     print 'Warning:'
  21372.     print 'you are installing the stored procedures '
  21373.     print 'on a pre 8.0 SQL Server.'
  21374.     print 'Ignore the following errors.'
  21375. end
  21376. go
  21377.  
  21378. /* 8.0 version */
  21379. create procedure sp_schemata_rowset
  21380.     (
  21381.     @schema_name    sysname = null, 
  21382.     @schema_owner    sysname = null
  21383.     )        
  21384. as
  21385.     select    distinct
  21386.         CATALOG_NAME            = db_name(),
  21387.         SCHEMA_NAME            = user_name(o.uid),    
  21388.         SCHEMA_OWNER            = user_name(o.uid),    
  21389.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,N'master'),
  21390.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,N'dbo'),
  21391.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,a_cha.name)
  21392.     from    
  21393.         sysobjects o,
  21394.         master.dbo.syscharsets        a_cha /* charset/1001, not sortorder */
  21395.     where   
  21396.         (@schema_name is null or @schema_name = user_name(o.uid))
  21397.     and     (@schema_owner is null or @schema_owner = user_name(o.uid))
  21398.     and    a_cha.type = 1001 /* type is charset */
  21399.     and     a_cha.id = convert(tinyint, DatabasePropertyEx(db_name(), 'sqlcharset')) /* what is charset of a table? */
  21400.     order by 2
  21401. go
  21402.  
  21403. dump tran master with no_log
  21404. go
  21405.  
  21406. /* The following stored procedure is used for Sphinx and Hydra */
  21407. create procedure sp_schemata_rowset;3
  21408. as
  21409.     select
  21410.         CATALOG_NAME            = convert(sysname,' '),
  21411.         SCHEMA_NAME            = convert(sysname,' '),    
  21412.         SCHEMA_OWNER            = convert(sysname,' '),    
  21413.         DEFAULT_CHARACTER_SET_CATALOG    = convert(sysname,' '),
  21414.         DEFAULT_CHARACTER_SET_SCHEMA    = convert(sysname,' '),
  21415.         DEFAULT_CHARACTER_SET_NAME    = convert(sysname,' ')
  21416.     where    1=0
  21417. go
  21418. grant execute on sp_schemata_rowset to public
  21419. go
  21420.  
  21421. dump tran master with no_log
  21422. go
  21423. if (charindex('6.00', @@version) > 0)
  21424.     begin
  21425.     if (exists (select * from sysobjects
  21426.             where name = 'sp_schemata_rowset' and type = 'P '))
  21427.         begin
  21428.         drop procedure sp_schemata_rowset
  21429.         dump tran master with no_log
  21430.         end
  21431.     end
  21432. go
  21433.  
  21434.  
  21435. print ''
  21436. print 'creating sp_statistics_rowset'
  21437. go
  21438.  
  21439.  
  21440. /*    6.0 and 6.5 version */
  21441. create procedure sp_statistics_rowset
  21442.     (
  21443.     @table_name    varchar(255),
  21444.     @table_schema    varchar(255) = null     
  21445.     )
  21446. as
  21447.     select    db_name()                as TABLE_CATALOG,        
  21448.         user_name(o.uid)            as TABLE_SCHEMA,
  21449.         o.name                    as TABLE_NAME,
  21450.         x.rows                    as CARDINALITY
  21451.     from    sysobjects o, sysindexes x, sysusers u
  21452.     where    o.type in ('U')
  21453.     and     o.name = @table_name
  21454.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21455.     and     x.id = o.id
  21456.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21457.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21458.     and     (
  21459.         suser_id() = 1     /* User is the System Administrator */
  21460.         or o.uid = user_id()     /* User created the object */
  21461.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21462.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21463.             from sysprotects p
  21464.             where p.id = o.id
  21465.             /* get rows for public,current user,user's group */
  21466.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21467.             /* check for SELECT,EXECUTE privilege */
  21468.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21469.             ) = 1    /* final magic...compare Grants      */
  21470.            )
  21471.     order by 2, 3
  21472. go
  21473. dump tran master with no_log
  21474. go
  21475. create procedure sp_statistics_rowset;2
  21476.     (
  21477.     @table_schema    varchar(255) = null     
  21478.     )
  21479. as
  21480.     select    db_name()                as TABLE_CATALOG,        
  21481.         user_name(o.uid)            as TABLE_SCHEMA,
  21482.         o.name                    as TABLE_NAME,
  21483.         x.rows                    as CARDINALITY
  21484.     from    sysobjects o, sysindexes x, sysusers u
  21485.     where    o.type in ('U')
  21486.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21487.     and     x.id = o.id
  21488.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21489.     and     (
  21490.         suser_id() = 1     /* User is the System Administrator */
  21491.         or o.uid = user_id()     /* User created the object */
  21492.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21493.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21494.             from sysprotects p
  21495.             where p.id = o.id
  21496.             /* get rows for public,current user,user's group */
  21497.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21498.             /* check for SELECT,EXECUTE privilege */
  21499.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21500.             ) = 1    /* final magic...compare Grants      */
  21501.            )        
  21502.     order by 2, 3
  21503. go
  21504. dump tran master with no_log
  21505. go
  21506.  
  21507. if (charindex('7.00', @@version) > 0 or
  21508.     charindex('8.00', @@version) > 0)
  21509.     drop procedure sp_statistics_rowset
  21510. else
  21511. begin
  21512.     print ''
  21513.     print ''
  21514.     print 'Warning:'
  21515.     print 'you are installing the stored procedures '
  21516.     print 'on a pre 8.0 SQL Server.'
  21517.     print 'Ignore the following errors.'
  21518. end
  21519. go
  21520.  
  21521. /*    8.0 version */
  21522. create procedure sp_statistics_rowset
  21523.     (
  21524.     @table_name    sysname,
  21525.     @table_schema    sysname = null     
  21526.     )
  21527. as
  21528.     select    db_name()                as TABLE_CATALOG,        
  21529.         user_name(o.uid)            as TABLE_SCHEMA,
  21530.         o.name                    as TABLE_NAME,
  21531.         x.rows                    as CARDINALITY
  21532.     from    sysobjects o, sysindexes x
  21533.     where    o.type in ('U')
  21534.     and     o.name = @table_name
  21535.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21536.     and     x.id = o.id
  21537.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21538.     and     permissions(o.id) <> 0
  21539.     order by 2, 3
  21540. go
  21541. dump tran master with no_log
  21542. go
  21543. create procedure sp_statistics_rowset;2
  21544.     (
  21545.     @table_schema    sysname = null     
  21546.     )
  21547. as
  21548.     select    db_name()                as TABLE_CATALOG,        
  21549.         user_name(o.uid)            as TABLE_SCHEMA,
  21550.         o.name                    as TABLE_NAME,
  21551.         x.rows                    as CARDINALITY
  21552.     from    sysobjects o, sysindexes x
  21553.     where    o.type in ('U')
  21554.     and     (@table_schema is null or @table_schema = user_name(o.uid))
  21555.     and     x.id = o.id
  21556.     and     x.indid in (0,1)    /*If there are no indexes then table stats are in a row with indid =0 */
  21557.     and     permissions(o.id) <> 0
  21558.     order by 2, 3
  21559. go
  21560.  
  21561. grant execute on sp_statistics_rowset to public
  21562. go
  21563.  
  21564. dump tran master with no_log
  21565. go
  21566. if (charindex('6.00', @@version) > 0)
  21567.     begin
  21568.     if (exists (select * from sysobjects
  21569.             where name = 'sp_statistics_rowset' and type = 'P '))
  21570.         begin
  21571.         drop procedure sp_statistics_rowset
  21572.         dump tran master with no_log
  21573.         end
  21574.     end
  21575. go
  21576.  
  21577.  
  21578. print ''
  21579. print 'creating sp_tables_rowset'
  21580. go
  21581.  
  21582.  
  21583. /*    Procedure for 6.50 and earlier servers */
  21584. create procedure sp_tables_rowset
  21585.     (
  21586.     @table_name    varchar(255), 
  21587.     @table_schema    varchar(255) = null,    
  21588.     @table_type    varchar(255) = null 
  21589.     )
  21590. as
  21591.     select    TABLE_CATALOG    = db_name(),
  21592.         TABLE_SCHEMA    = user_name(o.uid),
  21593.         TABLE_NAME    = o.name,
  21594.         TABLE_TYPE    = convert(varchar(30),
  21595.                     case o.type 
  21596.                     when 'U' then 'TABLE'
  21597.                     when 'V' then 'VIEW'
  21598.                     when 'S' then 'SYSTEM TABLE'
  21599.                     end),
  21600.         TABLE_GUID    = convert(binary(16), null),
  21601.         DESCRIPTION    = convert(varchar(1), null),
  21602.         TABLE_PROPID    = convert(int,null),
  21603.         DATE_CREATED    = o.crdate,
  21604.         DATE_MODIFIED    = convert(datetime,null)
  21605.     from    sysusers u, sysobjects o
  21606.     where    o.type in ('U','V','S')
  21607.     and     o.name = @table_name
  21608.     and     (    @table_schema is null
  21609.         or    @table_schema = user_name(o.uid)
  21610.         )
  21611.     and     (
  21612.             @table_type is null
  21613.         or    @table_type = case o.type 
  21614.                     when 'U' then 'TABLE'
  21615.                     when 'V' then 'VIEW'
  21616.                     when 'S' then 'SYSTEM TABLE'
  21617.                     end
  21618.         )
  21619.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21620.     and     (
  21621.         suser_id() = 1     /* User is the System Administrator */
  21622.         or o.uid = user_id()     /* User created the object */
  21623.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21624.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21625.             from sysprotects p
  21626.             /* join to correlate with all rows in sysobjects */
  21627.             where p.id = o.id
  21628.             /* get rows for public,current user,user's group */
  21629.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21630.             /* check for SELECT,EXECUTE privilege */
  21631.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21632.             ) = 1    /* final magic...compare Grants      */
  21633.         )        
  21634.     order by 4, 2, 3
  21635. go
  21636. dump tran master with no_log
  21637. go
  21638. create procedure sp_tables_rowset;2
  21639.     (
  21640.     @table_schema    varchar(255) = null,    
  21641.     @table_type    varchar(255) = null 
  21642.     )
  21643. as
  21644.     select    TABLE_CATALOG    = db_name(),
  21645.         TABLE_SCHEMA    = user_name(o.uid),
  21646.         TABLE_NAME    = o.name,
  21647.         TABLE_TYPE    = convert(varchar(30),
  21648.                     case o.type 
  21649.                     when 'U' then 'TABLE'
  21650.                     when 'V' then 'VIEW'
  21651.                     when 'S' then 'SYSTEM TABLE'
  21652.                     end),
  21653.         TABLE_GUID    = convert(binary(16), null),
  21654.         DESCRIPTION    = convert(varchar(1), null),
  21655.         TABLE_PROPID    = convert(int,null),
  21656.         DATE_CREATED    = o.crdate,
  21657.         DATE_MODIFIED    = convert(datetime,null)
  21658.     from    sysusers u, sysobjects o
  21659.     where    o.type in ('U','V','S')
  21660.     and     (    @table_schema is null
  21661.         or    @table_schema = user_name(o.uid)
  21662.         )
  21663.     and     (
  21664.             @table_type is null
  21665.         or    @table_type = case o.type 
  21666.                     when 'U' then 'TABLE'
  21667.                     when 'V' then 'VIEW'
  21668.                     when 'S' then 'SYSTEM TABLE'
  21669.                     end
  21670.         )
  21671.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21672.     and     (
  21673.         suser_id() = 1     /* User is the System Administrator */
  21674.         or o.uid = user_id()     /* User created the object */
  21675.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21676.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21677.             from sysprotects p
  21678.             /* join to correlate with all rows in sysobjects */
  21679.             where p.id = o.id
  21680.             /* get rows for public,current user,user's group */
  21681.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21682.             /* check for SELECT,EXECUTE privilege */
  21683.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21684.             ) = 1    /* final magic...compare Grants      */
  21685.         )        
  21686.     order by 4, 2, 3
  21687. go
  21688. dump tran master with no_log
  21689. go
  21690.  
  21691. if (charindex('7.00', @@version) > 0 or
  21692.     charindex('8.00', @@version) > 0)
  21693.     drop procedure sp_tables_rowset
  21694. else
  21695. begin
  21696.     print ''
  21697.     print ''
  21698.     print 'Warning:'
  21699.     print 'you are installing the stored procedures '
  21700.     print 'on a pre 8.0 SQL Server.'
  21701.     print 'Ignore the following errors.'
  21702. end
  21703. go
  21704.  
  21705. /*    Procedure for 8.0 server */
  21706. create procedure sp_tables_rowset
  21707.     (
  21708.     @table_name    sysname, 
  21709.     @table_schema    sysname = null,    
  21710.     @table_type    nvarchar(255) = null 
  21711.     )
  21712. as
  21713.     select    *
  21714.     from    (select    TABLE_CATALOG    = db_name(),
  21715.             TABLE_SCHEMA    = user_name(uid),
  21716.             TABLE_NAME    = name,
  21717.             TABLE_TYPE    = convert(nvarchar(30),
  21718.                         case type 
  21719.                         when 'U' then 
  21720.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21721.                             then N'TABLE' else N'SYSTEM TABLE' end
  21722.                         when 'S' then N'SYSTEM TABLE'
  21723.                         when 'V' then 
  21724.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21725.                             then N'VIEW' else N'SYSTEM VIEW' end
  21726.                         end),
  21727.             TABLE_GUID    = convert(uniqueidentifier, null),
  21728.             DESCRIPTION    = convert(nvarchar(1), null),
  21729.             TABLE_PROPID    = convert(int,null),
  21730.             DATE_CREATED    = crdate,
  21731.             DATE_MODIFIED    = convert(datetime,null)
  21732.         from    sysobjects
  21733.         where    name = @table_name
  21734.         and    type in ('U','V','S')
  21735.         and    permissions(id) <> 0
  21736.         ) as o
  21737.  
  21738.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21739.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  21740.     order by 4, 2, 3
  21741. go
  21742. dump tran master with no_log
  21743. go
  21744. create procedure sp_tables_rowset;2
  21745.     (
  21746.     @table_schema    sysname = null,    
  21747.     @table_type    nvarchar(255) = null 
  21748.     )
  21749. as
  21750.     select    *
  21751.     from    (select    TABLE_CATALOG    = db_name(),
  21752.             TABLE_SCHEMA    = user_name(uid),
  21753.             TABLE_NAME    = name,
  21754.             TABLE_TYPE    = convert(nvarchar(30),
  21755.                         case type 
  21756.                         when 'U' then 
  21757.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21758.                             then N'TABLE' else N'SYSTEM TABLE' end
  21759.                         when 'S' then N'SYSTEM TABLE'
  21760.                         when 'V' then 
  21761.                             case when ObjectProperty(id, 'IsMSShipped') = 0
  21762.                             then N'VIEW' else N'SYSTEM VIEW' end
  21763.                         end),
  21764.             TABLE_GUID    = convert(uniqueidentifier, null),
  21765.             DESCRIPTION    = convert(nvarchar(1), null),
  21766.             TABLE_PROPID    = convert(int,null),
  21767.             DATE_CREATED    = crdate,
  21768.             DATE_MODIFIED    = convert(datetime,null)
  21769.         from    sysobjects
  21770.         where    type in ('U','V','S')
  21771.         and    permissions(id) <> 0
  21772.         ) as o
  21773.  
  21774.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  21775.     and     (@table_type is null or @table_type = TABLE_TYPE)    
  21776.     order by 4, 2, 3
  21777. go
  21778. dump tran master with no_log
  21779. go
  21780. create procedure sp_tables_rowset;5
  21781.     (
  21782.     @table_server        sysname,
  21783.     @table_catalog        sysname = null,
  21784.     @table_name        sysname = null,
  21785.     @table_schema        sysname = null,
  21786.     @table_type        sysname = null
  21787.     )
  21788. as
  21789.     select
  21790.         TABLE_CATALOG,
  21791.         TABLE_SCHEMA,
  21792.         TABLE_NAME,
  21793.         TABLE_TYPE,
  21794.         TABLE_GUID,
  21795.         DESCRIPTION
  21796.     --    TABLE_PROPID,
  21797.     --    DATE_CREATED,
  21798.     --    DATE_MODIFIED
  21799.     from master.dbo.SYSREMOTE_TABLES <
  21800.                 @table_server,
  21801.                 @table_catalog,
  21802.                 @table_schema,
  21803.                 @table_name,
  21804.                 @table_type >
  21805.     order by 4,1,2,3
  21806. go
  21807.  
  21808. grant execute on sp_tables_rowset to public
  21809. go
  21810.  
  21811. dump tran master with no_log
  21812. go
  21813. if (charindex('6.00', @@version) > 0)
  21814.     begin
  21815.     if (exists (select * from sysobjects
  21816.             where name = 'sp_tables_rowset' and type = 'P '))
  21817.         begin
  21818.         drop procedure sp_tables_rowset
  21819.         dump tran master with no_log
  21820.         end
  21821.     end
  21822. go
  21823.  
  21824.  
  21825.  
  21826. print ''
  21827. print 'creating sp_tables_info_rowset'
  21828. go
  21829.  
  21830.  
  21831. /*    Procedure for 6.50 and earlier servers */
  21832. create procedure sp_tables_info_rowset
  21833.     (
  21834.     @table_name    varchar(255), 
  21835.     @table_schema    varchar(255) = null,    
  21836.     @table_type    varchar(255) = null 
  21837.     )
  21838. as
  21839.     select    TABLE_CATALOG        = db_name(),
  21840.         TABLE_SCHEMA        = user_name(o.uid),
  21841.         TABLE_NAME        = o.name,
  21842.         TABLE_TYPE        = convert(varchar(30),
  21843.                         case o.type 
  21844.                         when 'U' then 'TABLE'
  21845.                         when 'V' then 'VIEW'
  21846.                         when 'S' then 'SYSTEM TABLE'
  21847.                         end),
  21848.         TABLE_GUID        = convert(binary(16), null),
  21849.         BOOKMARKS        = convert(bit, 1),
  21850.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21851.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21852.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21853.         BOOKMARK_INFORMATION    = convert(int, 0),
  21854.         TABLE_VERSION        = convert(int, o.schema_ver),
  21855.         CARDINALITY        = x.rows,
  21856.         DESCRIPTION        = convert(varchar(1), null),
  21857.         TABLE_PROPID        = convert(int, null)
  21858.  
  21859.     from    sysusers u, 
  21860.         sysobjects o,
  21861.         sysindexes x
  21862.  
  21863.     where    o.type in ('U','V','S')
  21864.     and     o.name = @table_name
  21865.     and     (    @table_schema is null
  21866.         or    @table_schema = user_name(o.uid)
  21867.         )
  21868.     and     (
  21869.             @table_type is null
  21870.         or    @table_type = case o.type 
  21871.                     when 'U' then 'TABLE'
  21872.                     when 'V' then 'VIEW'
  21873.                     when 'S' then 'SYSTEM TABLE'
  21874.                     end
  21875.         )
  21876.     and    o.id *= x.id
  21877.     and    x.indid in (0,1)
  21878.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21879.     and     (
  21880.         suser_id() = 1     /* User is the System Administrator */
  21881.         or o.uid = user_id()     /* User created the object */
  21882.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21883.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21884.             from sysprotects p
  21885.             /* join to correlate with all rows in sysobjects */
  21886.             where p.id = o.id
  21887.             /* get rows for public,current user,user's group */
  21888.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21889.             /* check for SELECT,EXECUTE privilege */
  21890.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21891.             ) = 1    /* final magic...compare Grants      */
  21892.         )        
  21893.     order by 4, 2, 3
  21894. go
  21895. dump tran master with no_log
  21896. go
  21897. create procedure sp_tables_info_rowset;2
  21898.     (
  21899.     @table_schema    varchar(255) = null,    
  21900.     @table_type    varchar(255) = null 
  21901.     )
  21902. as
  21903.     select    TABLE_CATALOG        = db_name(),
  21904.         TABLE_SCHEMA        = user_name(o.uid),
  21905.         TABLE_NAME        = o.name,
  21906.         TABLE_TYPE        = convert(varchar(30),
  21907.                         case o.type 
  21908.                         when 'U' then 'TABLE'
  21909.                         when 'V' then 'VIEW'
  21910.                         when 'S' then 'SYSTEM TABLE'
  21911.                         end),
  21912.         TABLE_GUID        = convert(binary(16), null),
  21913.         BOOKMARKS        = convert(bit, 1),
  21914.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  21915.         BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  21916.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  21917.         BOOKMARK_INFORMATION    = convert(int, 0),
  21918.         TABLE_VERSION        = convert(int, o.schema_ver),
  21919.         CARDINALITY        = x.rows,
  21920.         DESCRIPTION        = convert(varchar(1), null),
  21921.         TABLE_PROPID        = convert(int, null)
  21922.  
  21923.     from    sysusers u, 
  21924.         sysobjects o,
  21925.         sysindexes x
  21926.  
  21927.     where    o.type in ('U','V','S')
  21928.     and     (    @table_schema is null
  21929.         or    @table_schema = user_name(o.uid)
  21930.         )
  21931.     and     (
  21932.             @table_type is null
  21933.         or    @table_type = case o.type 
  21934.                     when 'U' then 'TABLE'
  21935.                     when 'V' then 'VIEW'
  21936.                     when 'S' then 'SYSTEM TABLE'
  21937.                     end
  21938.         )
  21939.     and    o.id *= x.id
  21940.     and    x.indid in (0,1)
  21941.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  21942.     and     (
  21943.         suser_id() = 1     /* User is the System Administrator */
  21944.         or o.uid = user_id()     /* User created the object */
  21945.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  21946.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  21947.             from sysprotects p
  21948.             /* join to correlate with all rows in sysobjects */
  21949.             where p.id = o.id
  21950.             /* get rows for public,current user,user's group */
  21951.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  21952.             /* check for SELECT,EXECUTE privilege */
  21953.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  21954.             ) = 1    /* final magic...compare Grants      */
  21955.         )        
  21956.     order by 4, 2, 3
  21957. go
  21958. dump tran master with no_log
  21959. go
  21960.  
  21961. if (charindex('7.00', @@version) > 0 or
  21962.     charindex('8.00', @@version) > 0)
  21963.     drop procedure sp_tables_info_rowset
  21964. else
  21965. begin
  21966.     print ''
  21967.     print ''
  21968.     print 'Warning:'
  21969.     print 'you are installing the stored procedures '
  21970.     print 'on a pre 7.0 SQL Server.'
  21971.     print 'Ignore the following errors.'
  21972. end
  21973. go
  21974.  
  21975. /*    Procedure for 7.0 server */
  21976. create procedure sp_tables_info_rowset
  21977.     (
  21978.     @table_name    sysname, 
  21979.     @table_schema    sysname = null,    
  21980.     @table_type    nvarchar(255) = null 
  21981.     )
  21982. as
  21983.     select    *
  21984.     from    (select    TABLE_CATALOG        = db_name(),
  21985.             TABLE_SCHEMA        = user_name(o.uid),
  21986.             TABLE_NAME        = o.name,
  21987.             TABLE_TYPE        = convert(nvarchar(30),
  21988.                             case o.type 
  21989.                             when 'U' then 
  21990.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21991.                                 then N'TABLE' else N'SYSTEM TABLE' end
  21992.                             when 'S' then N'SYSTEM TABLE'
  21993.                             when 'V' then 
  21994.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  21995.                                 then N'VIEW' else N'SYSTEM VIEW' end
  21996.                             end),
  21997.             TABLE_GUID        = convert(uniqueidentifier, null),
  21998.             BOOKMARKS        = convert(bit, 1),
  21999.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22000.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  22001.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22002.             BOOKMARK_INFORMATION    = convert(int, 0),
  22003.             TABLE_VERSION        = convert(int, o.schema_ver),
  22004.             CARDINALITY        = x.rows,
  22005.             DESCRIPTION        = convert(nvarchar(1), null),
  22006.             TABLE_PROPID        = convert(int, null) 
  22007.     
  22008.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22009.         where    o.name = @table_name
  22010.         and     o.type in ('U','V','S')
  22011.         and    permissions(o.id) <> 0) as t
  22012.  
  22013.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22014.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22015.     order by 4, 2, 3
  22016. go
  22017. dump tran master with no_log
  22018. go
  22019. create procedure sp_tables_info_rowset;2
  22020.     (
  22021.     @table_schema    sysname = null,    
  22022.     @table_type    nvarchar(255) = null 
  22023.     )
  22024. as
  22025.     select    *
  22026.     from    (select    TABLE_CATALOG        = db_name(),
  22027.             TABLE_SCHEMA        = user_name(o.uid),
  22028.             TABLE_NAME        = o.name,
  22029.             TABLE_TYPE        = convert(nvarchar(30),
  22030.                             case o.type 
  22031.                             when 'U' then 
  22032.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22033.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22034.                             when 'S' then N'SYSTEM TABLE'
  22035.                             when 'V' then 
  22036.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22037.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22038.                             end),
  22039.             TABLE_GUID        = convert(uniqueidentifier, null),
  22040.             BOOKMARKS        = convert(bit, 1),
  22041.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22042.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  22043.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22044.             BOOKMARK_INFORMATION    = convert(int, 0),
  22045.             TABLE_VERSION        = convert(int, o.schema_ver),
  22046.             CARDINALITY        = x.rows,
  22047.             DESCRIPTION        = convert(nvarchar(1), null),
  22048.             TABLE_PROPID        = convert(int, null) 
  22049.     
  22050.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22051.         where     o.type in ('U','V','S')
  22052.         and    permissions(o.id) <> 0) as t
  22053.  
  22054.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22055.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22056.     order by 4, 2, 3
  22057. go
  22058.  
  22059. dump tran master with no_log
  22060. go
  22061.  
  22062. if (charindex('8.00', @@version) > 0)
  22063.     drop procedure sp_tables_info_rowset
  22064. else
  22065. begin
  22066.     print ''
  22067.     print ''
  22068.     print 'Warning:'
  22069.     print 'you are installing the stored procedures '
  22070.     print 'on a pre 8.0 SQL Server.'
  22071.     print 'Ignore the following errors.'
  22072. end
  22073. go
  22074.  
  22075. /*    Procedure for 8.0 server */
  22076. create procedure sp_tables_info_rowset
  22077.     (
  22078.     @table_name    sysname, 
  22079.     @table_schema    sysname = null,    
  22080.     @table_type    nvarchar(255) = null 
  22081.     )
  22082. as
  22083.     select    *
  22084.     from    (select    TABLE_CATALOG        = db_name(),
  22085.             TABLE_SCHEMA        = user_name(o.uid),
  22086.             TABLE_NAME        = o.name,
  22087.             TABLE_TYPE        = convert(nvarchar(30),
  22088.                             case o.type 
  22089.                             when 'U' then 
  22090.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22091.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22092.                             when 'S' then N'SYSTEM TABLE'
  22093.                             when 'V' then 
  22094.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22095.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22096.                             end),
  22097.             TABLE_GUID        = convert(uniqueidentifier, null),
  22098.             BOOKMARKS        = convert(bit, 1),
  22099.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22100.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  22101.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22102.             BOOKMARK_INFORMATION    = convert(int, 0),
  22103.             TABLE_VERSION        = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22104.             CARDINALITY        = x.rows,
  22105.             DESCRIPTION        = convert(nvarchar(1), null),
  22106.             TABLE_PROPID        = convert(int, null) 
  22107.     
  22108.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22109.         where    o.name = @table_name
  22110.         and     o.type in ('U','V','S')
  22111.         and    permissions(o.id) <> 0) as t
  22112.  
  22113.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22114.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22115.     order by 4, 2, 3
  22116. go
  22117. dump tran master with no_log
  22118. go
  22119. create procedure sp_tables_info_rowset;2
  22120.     (
  22121.     @table_schema    sysname = null,    
  22122.     @table_type    nvarchar(255) = null 
  22123.     )
  22124. as
  22125.     select    *
  22126.     from    (select    TABLE_CATALOG        = db_name(),
  22127.             TABLE_SCHEMA        = user_name(o.uid),
  22128.             TABLE_NAME        = o.name,
  22129.             TABLE_TYPE        = convert(nvarchar(30),
  22130.                             case o.type 
  22131.                             when 'U' then 
  22132.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22133.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22134.                             when 'S' then N'SYSTEM TABLE'
  22135.                             when 'V' then 
  22136.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22137.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22138.                             end),
  22139.             TABLE_GUID        = convert(uniqueidentifier, null),
  22140.             BOOKMARKS        = convert(bit, 1),
  22141.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22142.             BOOKMARK_DATATYPE    = convert(smallint, 19 /*DBTYPE_UI4 */),
  22143.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 4),
  22144.             BOOKMARK_INFORMATION    = convert(int, 0),
  22145.             TABLE_VERSION        = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22146.             CARDINALITY        = x.rows,
  22147.             DESCRIPTION        = convert(nvarchar(1), null),
  22148.             TABLE_PROPID        = convert(int, null) 
  22149.     
  22150.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22151.         where     o.type in ('U','V','S')
  22152.         and    permissions(o.id) <> 0) as t
  22153.  
  22154.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22155.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22156.     order by 4, 2, 3
  22157. go
  22158.  
  22159. grant execute on sp_tables_info_rowset to public
  22160. go
  22161.  
  22162. dump tran master with no_log
  22163. go
  22164.  
  22165. if (charindex('6.00', @@version) > 0)
  22166.     begin
  22167.     if (exists (select * from sysobjects
  22168.         where name = 'sp_tables_info_rowset' and type = 'P '))
  22169.         begin
  22170.         drop procedure sp_tables_info_rowset
  22171.         dump tran master with no_log
  22172.         end
  22173.     end
  22174. go
  22175.  
  22176. print ''
  22177. print 'creating sp_tables_info_rowset_64'
  22178. go
  22179.  
  22180.  
  22181. /*    Procedure for 6.50 and earlier servers */
  22182. create procedure sp_tables_info_rowset_64
  22183.     (
  22184.     @table_name    varchar(255), 
  22185.     @table_schema    varchar(255) = null,    
  22186.     @table_type    varchar(255) = null 
  22187.     )
  22188. as
  22189.     select    TABLE_CATALOG        = db_name(),
  22190.         TABLE_SCHEMA        = user_name(o.uid),
  22191.         TABLE_NAME        = o.name,
  22192.         TABLE_TYPE        = convert(varchar(30),
  22193.                         case o.type 
  22194.                         when 'U' then 'TABLE'
  22195.                         when 'V' then 'VIEW'
  22196.                         when 'S' then 'SYSTEM TABLE'
  22197.                         end),
  22198.         TABLE_GUID        = convert(binary(16), null),
  22199.         BOOKMARKS        = convert(bit, 1),
  22200.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22201.         BOOKMARK_DATATYPE    = convert(smallint, 21 /*DBTYPE_UI8 */),
  22202.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22203.         BOOKMARK_INFORMATION    = convert(int, 0),
  22204.         TABLE_VERSION        = convert(int, o.schema_ver),
  22205.         CARDINALITY        = x.rows,
  22206.         DESCRIPTION        = convert(varchar(1), null),
  22207.         TABLE_PROPID        = convert(int, null)
  22208.  
  22209.     from    sysusers u, 
  22210.         sysobjects o,
  22211.         sysindexes x
  22212.  
  22213.     where    o.type in ('U','V','S')
  22214.     and     o.name = @table_name
  22215.     and     (    @table_schema is null
  22216.         or    @table_schema = user_name(o.uid)
  22217.         )
  22218.     and     (
  22219.             @table_type is null
  22220.         or    @table_type = case o.type 
  22221.                     when 'U' then 'TABLE'
  22222.                     when 'V' then 'VIEW'
  22223.                     when 'S' then 'SYSTEM TABLE'
  22224.                     end
  22225.         )
  22226.     and    o.id *= x.id
  22227.     and    x.indid in (0,1)
  22228.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22229.     and     (
  22230.         suser_id() = 1     /* User is the System Administrator */
  22231.         or o.uid = user_id()     /* User created the object */
  22232.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22233.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22234.             from sysprotects p
  22235.             /* join to correlate with all rows in sysobjects */
  22236.             where p.id = o.id
  22237.             /* get rows for public,current user,user's group */
  22238.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22239.             /* check for SELECT,EXECUTE privilege */
  22240.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22241.             ) = 1    /* final magic...compare Grants      */
  22242.         )        
  22243.     order by 4, 2, 3
  22244. go
  22245. dump tran master with no_log
  22246. go
  22247. create procedure sp_tables_info_rowset_64;2
  22248.     (
  22249.     @table_schema    varchar(255) = null,    
  22250.     @table_type    varchar(255) = null 
  22251.     )
  22252. as
  22253.     select    TABLE_CATALOG        = db_name(),
  22254.         TABLE_SCHEMA        = user_name(o.uid),
  22255.         TABLE_NAME        = o.name,
  22256.         TABLE_TYPE        = convert(varchar(30),
  22257.                         case o.type 
  22258.                         when 'U' then 'TABLE'
  22259.                         when 'V' then 'VIEW'
  22260.                         when 'S' then 'SYSTEM TABLE'
  22261.                         end),
  22262.         TABLE_GUID        = convert(binary(16), null),
  22263.         BOOKMARKS        = convert(bit, 1),
  22264.         BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22265.         BOOKMARK_DATATYPE    = convert(smallint, 21 /*DBTYPE_UI8 */),
  22266.         BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22267.         BOOKMARK_INFORMATION    = convert(int, 0),
  22268.         TABLE_VERSION        = convert(int, o.schema_ver),
  22269.         CARDINALITY        = x.rows,
  22270.         DESCRIPTION        = convert(varchar(1), null),
  22271.         TABLE_PROPID        = convert(int, null)
  22272.  
  22273.     from    sysusers u, 
  22274.         sysobjects o,
  22275.         sysindexes x
  22276.  
  22277.     where    o.type in ('U','V','S')
  22278.     and     (    @table_schema is null
  22279.         or    @table_schema = user_name(o.uid)
  22280.         )
  22281.     and     (
  22282.             @table_type is null
  22283.         or    @table_type = case o.type 
  22284.                     when 'U' then 'TABLE'
  22285.                     when 'V' then 'VIEW'
  22286.                     when 'S' then 'SYSTEM TABLE'
  22287.                     end
  22288.         )
  22289.     and    o.id *= x.id
  22290.     and    x.indid in (0,1)
  22291.     and     u.uid = user_id() /* constrain sysusers uid for use in subquery */
  22292.     and     (
  22293.         suser_id() = 1     /* User is the System Administrator */
  22294.         or o.uid = user_id()     /* User created the object */
  22295.         /* here's the magic... select the highest precedence of permissions in the order (user,group,public)  */
  22296.         or (    (select max(((sign(uid)*abs(uid-16383))*2)+(protecttype&1))
  22297.             from sysprotects p
  22298.             /* join to correlate with all rows in sysobjects */
  22299.             where p.id = o.id
  22300.             /* get rows for public,current user,user's group */
  22301.             and (p.uid = 0 or p.uid = user_id() or p.uid = u.gid)
  22302.             /* check for SELECT,EXECUTE privilege */
  22303.             and (action in (193,224)))&1     /* more magic...normalize GRANT */
  22304.             ) = 1    /* final magic...compare Grants      */
  22305.         )        
  22306.     order by 4, 2, 3
  22307. go
  22308. dump tran master with no_log
  22309. go
  22310.  
  22311. if (charindex('7.00', @@version) > 0 or
  22312.     charindex('8.00', @@version) > 0)
  22313.     drop procedure sp_tables_info_rowset_64
  22314. else
  22315. begin
  22316.     print ''
  22317.     print ''
  22318.     print 'Warning:'
  22319.     print 'you are installing the stored procedures '
  22320.     print 'on a pre 7.0 SQL Server.'
  22321.     print 'Ignore the following errors.'
  22322. end
  22323. go
  22324.  
  22325. /*    Procedure for 7.0 server */
  22326. create procedure sp_tables_info_rowset_64
  22327.     (
  22328.     @table_name    sysname, 
  22329.     @table_schema    sysname = null,    
  22330.     @table_type    nvarchar(255) = null 
  22331.     )
  22332. as
  22333.     select    *
  22334.     from    (select    TABLE_CATALOG        = db_name(),
  22335.             TABLE_SCHEMA        = user_name(o.uid),
  22336.             TABLE_NAME        = o.name,
  22337.             TABLE_TYPE        = convert(nvarchar(30),
  22338.                             case o.type 
  22339.                             when 'U' then 
  22340.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22341.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22342.                             when 'S' then N'SYSTEM TABLE'
  22343.                             when 'V' then 
  22344.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22345.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22346.                             end),
  22347.             TABLE_GUID        = convert(uniqueidentifier, null),
  22348.             BOOKMARKS        = convert(bit, 1),
  22349.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22350.             BOOKMARK_DATATYPE    = convert(smallint, 21 /*DBTYPE_UI8 */),
  22351.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22352.             BOOKMARK_INFORMATION    = convert(int, 0),
  22353.             TABLE_VERSION        = convert(int, o.schema_ver),
  22354.             CARDINALITY        = x.rows,
  22355.             DESCRIPTION        = convert(nvarchar(1), null),
  22356.             TABLE_PROPID        = convert(int, null) 
  22357.     
  22358.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22359.         where    o.name = @table_name
  22360.         and     o.type in ('U','V','S')
  22361.         and    permissions(o.id) <> 0) as t
  22362.  
  22363.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22364.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22365.     order by 4, 2, 3
  22366. go
  22367. dump tran master with no_log
  22368. go
  22369. create procedure sp_tables_info_rowset_64;2
  22370.     (
  22371.     @table_schema    sysname = null,    
  22372.     @table_type    nvarchar(255) = null 
  22373.     )
  22374. as
  22375.     select    *
  22376.     from    (select    TABLE_CATALOG        = db_name(),
  22377.             TABLE_SCHEMA        = user_name(o.uid),
  22378.             TABLE_NAME        = o.name,
  22379.             TABLE_TYPE        = convert(nvarchar(30),
  22380.                             case o.type 
  22381.                             when 'U' then 
  22382.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22383.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22384.                             when 'S' then N'SYSTEM TABLE'
  22385.                             when 'V' then 
  22386.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22387.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22388.                             end),
  22389.             TABLE_GUID        = convert(uniqueidentifier, null),
  22390.             BOOKMARKS        = convert(bit, 1),
  22391.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22392.             BOOKMARK_DATATYPE    = convert(smallint, 21 /*DBTYPE_UI8 */),
  22393.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22394.             BOOKMARK_INFORMATION    = convert(int, 0),
  22395.             TABLE_VERSION        = convert(int, o.schema_ver),
  22396.             CARDINALITY        = x.rows,
  22397.             DESCRIPTION        = convert(nvarchar(1), null),
  22398.             TABLE_PROPID        = convert(int, null) 
  22399.     
  22400.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22401.         where     o.type in ('U','V','S')
  22402.         and    permissions(o.id) <> 0) as t
  22403.  
  22404.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22405.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22406.     order by 4, 2, 3
  22407. go
  22408.  
  22409. dump tran master with no_log
  22410. go
  22411.  
  22412. if (charindex('8.00', @@version) > 0)
  22413.     drop procedure sp_tables_info_rowset_64
  22414. else
  22415. begin
  22416.     print ''
  22417.     print ''
  22418.     print 'Warning:'
  22419.     print 'you are installing the stored procedures '
  22420.     print 'on a pre 8.0 SQL Server.'
  22421.     print 'Ignore the following errors.'
  22422. end
  22423. go
  22424.  
  22425. /*    Procedure for 8.0 server */
  22426. create procedure sp_tables_info_rowset_64
  22427.     (
  22428.     @table_name    sysname, 
  22429.     @table_schema    sysname = null,    
  22430.     @table_type    nvarchar(255) = null 
  22431.     )
  22432. as
  22433.     select    *
  22434.     from    (select    TABLE_CATALOG        = db_name(),
  22435.             TABLE_SCHEMA        = user_name(o.uid),
  22436.             TABLE_NAME        = o.name,
  22437.             TABLE_TYPE        = convert(nvarchar(30),
  22438.                             case o.type 
  22439.                             when 'U' then 
  22440.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22441.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22442.                             when 'S' then N'SYSTEM TABLE'
  22443.                             when 'V' then 
  22444.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22445.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22446.                             end),
  22447.             TABLE_GUID        = convert(uniqueidentifier, null),
  22448.             BOOKMARKS        = convert(bit, 1),
  22449.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22450.             BOOKMARK_DATATYPE    = convert(smallint, 21 /*DBTYPE_UI8 */),
  22451.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22452.             BOOKMARK_INFORMATION    = convert(int, 0),
  22453.             TABLE_VERSION        = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22454.             CARDINALITY        = x.rows,
  22455.             DESCRIPTION        = convert(nvarchar(1), null),
  22456.             TABLE_PROPID        = convert(int, null) 
  22457.     
  22458.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22459.         where    o.name = @table_name
  22460.         and     o.type in ('U','V','S')
  22461.         and    permissions(o.id) <> 0) as t
  22462.  
  22463.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22464.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22465.     order by 4, 2, 3
  22466. go
  22467. dump tran master with no_log
  22468. go
  22469. create procedure sp_tables_info_rowset_64;2
  22470.     (
  22471.     @table_schema    sysname = null,    
  22472.     @table_type    nvarchar(255) = null 
  22473.     )
  22474. as
  22475.     select    *
  22476.     from    (select    TABLE_CATALOG        = db_name(),
  22477.             TABLE_SCHEMA        = user_name(o.uid),
  22478.             TABLE_NAME        = o.name,
  22479.             TABLE_TYPE        = convert(nvarchar(30),
  22480.                             case o.type 
  22481.                             when 'U' then 
  22482.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22483.                                 then N'TABLE' else N'SYSTEM TABLE' end
  22484.                             when 'S' then N'SYSTEM TABLE'
  22485.                             when 'V' then 
  22486.                                 case when ObjectProperty(o.id, 'IsMSShipped') = 0
  22487.                                 then N'VIEW' else N'SYSTEM VIEW' end
  22488.                             end),
  22489.             TABLE_GUID        = convert(uniqueidentifier, null),
  22490.             BOOKMARKS        = convert(bit, 1),
  22491.             BOOKMARK_TYPE        = convert(int, 1 /*DBPROPVAL_BMK_NUMERIC*/),
  22492.             BOOKMARK_DATATYPE    = convert(smallint, 21 /*DBTYPE_UI8 */),
  22493.             BOOKMARK_MAXIMUM_LENGTH = convert(int, 8),
  22494.             BOOKMARK_INFORMATION    = convert(int, 0),
  22495.             TABLE_VERSION        = convert(bigint, ObjectPropertyEx(o.id, 'objectversion')),
  22496.             CARDINALITY        = x.rows,
  22497.             DESCRIPTION        = convert(nvarchar(1), null),
  22498.             TABLE_PROPID        = convert(int, null) 
  22499.     
  22500.         from    sysobjects o left join sysindexes x on o.id = x.id and x.indid in (0,1)
  22501.         where     o.type in ('U','V','S')
  22502.         and    permissions(o.id) <> 0) as t
  22503.  
  22504.     where     (@table_schema is null or @table_schema = TABLE_SCHEMA)
  22505.     and     (@table_type is null or    @table_type = TABLE_TYPE)
  22506.     order by 4, 2, 3
  22507. go
  22508.  
  22509. grant execute on sp_tables_info_rowset_64 to public
  22510. go
  22511.  
  22512. dump tran master with no_log
  22513. go
  22514.  
  22515. if (charindex('6.00', @@version) > 0)
  22516.     begin
  22517.     if (exists (select * from sysobjects
  22518.         where name = 'sp_tables_info_rowset_64' and type = 'P '))
  22519.         begin
  22520.         drop procedure sp_tables_info_rowset_64
  22521.         dump tran master with no_log
  22522.         end
  22523.     end
  22524. go
  22525.  
  22526. print ''
  22527. print 'creating sp_table_constraints_rowset'
  22528. go
  22529.  
  22530.  
  22531. /*    Procedure for 6.50 and earlier servers */
  22532. create procedure sp_table_constraints_rowset
  22533.     (
  22534.     @table_name            varchar(255), 
  22535.     @table_schema        varchar(255) = null,
  22536.     @table_catalog        varchar(255) = null,
  22537.     @constraint_name    varchar(255) = null,
  22538.     @constraint_schema    varchar(255) = null,
  22539.     @constraint_catalog    varchar(255) = null,
  22540.     @constraint_type    varchar(255) = null 
  22541.     )
  22542. as
  22543.     select
  22544.         CONSTRAINT_CATALOG    = db_name(),
  22545.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22546.         CONSTRAINT_NAME        = c_obj.name,
  22547.         TABLE_CATALOG        = db_name(),
  22548.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22549.         TABLE_NAME            = t_obj.name,
  22550.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22551.                                 when 1 then 'PRIMARY KEY'
  22552.                                 when 2 then    'UNIQUE'
  22553.                                 when 3 then    'FOREIGN KEY'
  22554.                                 when 4 then    'CHECK'
  22555.                                  end, 
  22556.         IS_DEFERRABLE        = convert(tinyint, 0),
  22557.         INITIALLY_DEFERRED    = convert(tinyint, 0),
  22558.         DESCRIPTION            = convert(varchar(1), null)
  22559.  
  22560.     from 
  22561.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22562.     where
  22563.             t_obj.name    = @table_name
  22564.         and t_obj.type in ('U','S')
  22565.         and (@table_catalog is null or @table_catalog = db_name())
  22566.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22567.         and c.id = t_obj.id
  22568.         and (c.status & 0xf) between 1 and 4
  22569.         and c_obj.id    = c.constid
  22570.         and    c_obj.uid    = user_id()
  22571.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22572.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22573.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22574.         and (@constraint_type is null
  22575.             or (c.status & 0xf)
  22576.                 = case @constraint_type
  22577.                     when 'PRIMARY KEY' then 1
  22578.                     when 'UNIQUE' then 2
  22579.                     when 'FOREIGN KEY' then 3
  22580.                     when 'CHECK' then 4
  22581.                     end)
  22582. order by 2,3,5,6,7 
  22583. go
  22584. dump tran master with no_log
  22585. go
  22586. create procedure sp_table_constraints_rowset;2
  22587.     (
  22588.     @table_schema        varchar(255) = null,
  22589.     @table_catalog        varchar(255) = null,
  22590.     @constraint_name    varchar(255) = null,
  22591.     @constraint_schema    varchar(255) = null,
  22592.     @constraint_catalog    varchar(255) = null,
  22593.     @constraint_type    varchar(255) = null 
  22594.     )
  22595. as
  22596.     select
  22597.         CONSTRAINT_CATALOG    = db_name(),
  22598.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22599.         CONSTRAINT_NAME        = c_obj.name,
  22600.         TABLE_CATALOG        = db_name(),
  22601.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22602.         TABLE_NAME            = t_obj.name,
  22603.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22604.                                 when 1 then 'PRIMARY KEY'
  22605.                                 when 2 then    'UNIQUE'
  22606.                                 when 3 then    'FOREIGN KEY'
  22607.                                 when 4 then    'CHECK'
  22608.                                  end, 
  22609.         IS_DEFERRABLE        = convert(tinyint, 0),
  22610.         INITIALLY_DEFERRED    = convert(tinyint, 0),
  22611.         DESCRIPTION            = convert(varchar(1), null)
  22612.  
  22613.     from 
  22614.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22615.     where
  22616.             t_obj.type in ('U','S')
  22617.         and (@table_catalog is null or @table_catalog = db_name())
  22618.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22619.         and c.id = t_obj.id
  22620.         and (c.status & 0xf) between 1 and 4
  22621.         and c_obj.id    = c.constid
  22622.         and    c_obj.uid    = user_id()
  22623.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22624.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22625.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22626.         and (@constraint_type is null
  22627.             or (c.status & 0xf)
  22628.                 = case @constraint_type
  22629.                     when 'PRIMARY KEY' then 1
  22630.                     when 'UNIQUE' then 2
  22631.                     when 'FOREIGN KEY' then 3
  22632.                     when 'CHECK' then 4
  22633.                     end)
  22634. order by 2,3,5,6,7 
  22635. go
  22636. dump tran master with no_log
  22637. go
  22638.  
  22639.  
  22640. if (charindex('7.00', @@version) > 0 or
  22641.     charindex('8.00', @@version) > 0)
  22642.     drop procedure sp_table_constraints_rowset
  22643. else
  22644. begin
  22645.     print ''
  22646.     print ''
  22647.     print 'Warning:'
  22648.     print 'you are installing the stored procedures '
  22649.     print 'on a pre 8.0 SQL Server.'
  22650.     print 'Ignore the following errors.'
  22651. end
  22652. go
  22653.  
  22654. /*    Procedure for 8.0 server */
  22655. create procedure sp_table_constraints_rowset
  22656.     (
  22657.     @table_name            sysname, 
  22658.     @table_schema        sysname = null,
  22659.     @table_catalog        sysname = null,
  22660.     @constraint_name    sysname = null,
  22661.     @constraint_schema    sysname = null,
  22662.     @constraint_catalog    sysname = null,
  22663.     @constraint_type    nvarchar(255) = null 
  22664.     )
  22665. as
  22666.     select
  22667.         CONSTRAINT_CATALOG    = db_name(),
  22668.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22669.         CONSTRAINT_NAME        = c_obj.name,
  22670.         TABLE_CATALOG        = db_name(),
  22671.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22672.         TABLE_NAME            = t_obj.name,
  22673.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22674.                                 when 1 then N'PRIMARY KEY'
  22675.                                 when 2 then    N'UNIQUE'
  22676.                                 when 3 then    N'FOREIGN KEY'
  22677.                                 when 4 then    N'CHECK'
  22678.                                  end, 
  22679.         IS_DEFERRABLE        = convert(bit, 0),
  22680.         INITIALLY_DEFERRED    = convert(bit, 0),
  22681.         DESCRIPTION            = convert(nvarchar(1), null)
  22682.  
  22683.     from 
  22684.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22685.     where
  22686.             t_obj.name    = @table_name
  22687.         and t_obj.type in ('U','S')
  22688.         and (@table_catalog is null or @table_catalog = db_name())
  22689.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22690.         and c.id = t_obj.id
  22691.         and (c.status & 0xf) between 1 and 4
  22692.         and c_obj.id    = c.constid
  22693.         and    c_obj.uid    = user_id()
  22694.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22695.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22696.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22697.         and (@constraint_type is null
  22698.             or (c.status & 0xf)
  22699.                 = case @constraint_type
  22700.                     when N'PRIMARY KEY' then 1
  22701.                     when N'UNIQUE' then 2
  22702.                     when N'FOREIGN KEY' then 3
  22703.                     when N'CHECK' then 4
  22704.                     end)
  22705. order by 2,3,5,6,7 
  22706. go
  22707. dump tran master with no_log
  22708. go
  22709. create procedure sp_table_constraints_rowset;2
  22710.     (
  22711.     @table_schema        sysname = null,
  22712.     @table_catalog        sysname = null,
  22713.     @constraint_name    sysname = null,
  22714.     @constraint_schema    sysname = null,
  22715.     @constraint_catalog    sysname = null,
  22716.     @constraint_type    nvarchar(255) = null 
  22717.     )
  22718. as
  22719.     select
  22720.         CONSTRAINT_CATALOG    = db_name(),
  22721.         CONSTRAINT_SCHEMA    = user_name(c_obj.uid),
  22722.         CONSTRAINT_NAME        = c_obj.name,
  22723.         TABLE_CATALOG        = db_name(),
  22724.         TABLE_SCHEMA        = user_name(t_obj.uid),
  22725.         TABLE_NAME            = t_obj.name,
  22726.         CONSTRAINT_TYPE        = case (c.status & 0xf)
  22727.                                 when 1 then N'PRIMARY KEY'
  22728.                                 when 2 then    N'UNIQUE'
  22729.                                 when 3 then    N'FOREIGN KEY'
  22730.                                 when 4 then    N'CHECK'
  22731.                                  end, 
  22732.         IS_DEFERRABLE        = convert(bit, 0),
  22733.         INITIALLY_DEFERRED    = convert(bit, 0),
  22734.         DESCRIPTION            = convert(nvarchar(1), null)
  22735.  
  22736.     from 
  22737.         sysobjects c_obj, sysobjects t_obj, sysconstraints c
  22738.     where
  22739.             t_obj.type in ('U','S')
  22740.         and (@table_catalog is null or @table_catalog = db_name())
  22741.         and    (@table_schema is null or @table_schema = user_name(t_obj.uid))
  22742.         and c.id = t_obj.id
  22743.         and (c.status & 0xf) between 1 and 4
  22744.         and c_obj.id    = c.constid
  22745.         and    c_obj.uid    = user_id()
  22746.         and (@constraint_name is null or c_obj.name    = @constraint_name)
  22747.         and (@constraint_catalog is null or @constraint_catalog = db_name())
  22748.         and    (@constraint_schema is null or @constraint_schema = user_name(c_obj.uid))
  22749.         and (@constraint_type is null
  22750.             or (c.status & 0xf)
  22751.                 = case @constraint_type
  22752.                     when N'PRIMARY KEY' then 1
  22753.                     when N'UNIQUE' then 2
  22754.                     when N'FOREIGN KEY' then 3
  22755.                     when N'CHECK' then 4
  22756.                     end)
  22757. order by 2,3,5,6,7 
  22758. go
  22759. dump tran master with no_log
  22760.  
  22761.  
  22762. grant execute on sp_table_constraints_rowset to public
  22763. go
  22764.  
  22765. dump tran master with no_log
  22766. go
  22767. if (charindex('6.00', @@version) > 0)
  22768.     begin
  22769.     if (exists (select * from sysobjects
  22770.             where name = 'sp_table_constraints_rowset' and type = 'P '))
  22771.         begin
  22772.         drop procedure sp_table_constraints_rowset
  22773.         dump tran master with no_log
  22774.         end
  22775.     end
  22776. go
  22777.  
  22778. print ''
  22779. print 'creating sp_table_privileges_rowset'
  22780. go
  22781.  
  22782. /*    Procedure for 6.0 and 6.5 server */
  22783. CREATE PROCEDURE sp_table_privileges_rowset
  22784.     (
  22785.     @table_name    varchar(255) = null,
  22786.     @table_schema    varchar(255) = null,
  22787.     @grantor    varchar(255) = null,
  22788.     @grantee    varchar(255) = null
  22789.     )
  22790. as
  22791. IF @table_name is not null
  22792.     BEGIN
  22793.     select
  22794.         GRANTOR        = user_name(p.grantor),
  22795.         GRANTEE        = user_name(u.uid),
  22796.         TABLE_CATALOG    = db_name(),
  22797.         TABLE_SCHEMA    = user_name(o.uid),
  22798.         TABLE_NAME    = o.name,
  22799.         PRIVILEGE_TYPE    = convert(varchar(30),
  22800.                     case p.action
  22801.                     when 193 then 'SELECT'
  22802.                     when 195 then 'INSERT'
  22803.                     when 196 then 'DELETE'
  22804.                     when 197 then 'UPDATE'
  22805.                     else 'REFERENCES'
  22806.                     end),
  22807.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22808.     from 
  22809.         sysprotects p, sysobjects o, sysusers u
  22810.     where
  22811.         o.name = @table_name
  22812.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22813.     and    (@grantee is null or @grantee = user_name(u.uid))
  22814.     and     o.type in ('U','V','S')
  22815.     and    p.id = o.id
  22816.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22817.             /* expand groups */
  22818.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22819.     and     p.protecttype <> 206    /* only grant rows */
  22820.     and     p.action in (26,193,195,196,197)
  22821.     and     o.uid <> u.uid            /* no rows for owner */
  22822.     and     not exists (            /* exclude revoke'd privileges */
  22823.             select     *
  22824.             from     sysprotects p1
  22825.             where    p1.protecttype = 206
  22826.             and     p1.action = p.action
  22827.             and     p1.id = p.id
  22828.             and     p1.uid = u.uid)
  22829.     union
  22830.     select    /*    Add rows for table owner */
  22831.         GRANTOR        = user_name(u.uid),
  22832.         GRANTEE        = user_name(o.uid),
  22833.         TABLE_CATALOG    = db_name(),
  22834.         TABLE_SCHEMA    = user_name(o.uid),
  22835.         TABLE_NAME    = o.name,
  22836.         PRIVILEGE_TYPE    = convert(varchar(30),
  22837.                     case v.number
  22838.                     when 193 then 'SELECT'
  22839.                     when 195 then 'INSERT'
  22840.                     when 196 then 'DELETE'
  22841.                     when 197 then 'UPDATE'
  22842.                     else 'REFERENCES'
  22843.                     end),
  22844.         IS_GRANTABLE    = convert(bit,1)    
  22845.     from 
  22846.         sysobjects o, master.dbo.spt_values v, sysusers u
  22847.     where
  22848.         o.name = @table_name
  22849.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22850.     and    (@grantee is null or @grantee = user_name(o.uid))
  22851.     and     o.type in ('U','V','S')
  22852.     and     u.uid = 1        /* grantor is dbo of database */
  22853.     and    (@grantor is null or @grantor = user_name(u.uid))
  22854.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22855.     and     v.number in (26,193,195,196,197)
  22856.     and     not exists (    /* exclude revoke'd privileges */
  22857.             select     *
  22858.             from     sysprotects p1
  22859.             where    p1.protecttype = 206
  22860.             and     p1.action = v.number
  22861.             and     p1.id = o.id
  22862.             and     p1.uid = o.uid)
  22863.     order by 4,5,6,1,2
  22864.     END
  22865. ELSE
  22866.     BEGIN
  22867.     select
  22868.         GRANTOR        = user_name(p.grantor),
  22869.         GRANTEE        = user_name(u.uid),
  22870.         TABLE_CATALOG    = db_name(),
  22871.         TABLE_SCHEMA    = user_name(o.uid),
  22872.         TABLE_NAME    = o.name,
  22873.         PRIVILEGE_TYPE    = convert(varchar(30),
  22874.                     case p.action
  22875.                     when 193 then 'SELECT'
  22876.                     when 195 then 'INSERT'
  22877.                     when 196 then 'DELETE'
  22878.                     when 197 then 'UPDATE'
  22879.                     else 'REFERENCES'
  22880.                     end),
  22881.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22882.     from 
  22883.         sysprotects p, sysobjects o, sysusers u
  22884.     where
  22885.         o.type in ('U','V','S')
  22886.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22887.     and    (@grantee is null or @grantee = user_name(u.uid))
  22888.     and    p.id = o.id
  22889.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22890.             /* expand groups */
  22891.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22892.     and     p.protecttype <> 206    /* only grant rows */
  22893.     and     p.action in (26,193,195,196,197)
  22894.     and     o.uid <> u.uid            /* no rows for owner */
  22895.     and     not exists (            /* exclude revoke'd privileges */
  22896.             select     *
  22897.             from     sysprotects p1
  22898.             where    p1.protecttype = 206
  22899.             and     p1.action = p.action
  22900.             and     p1.id = p.id
  22901.             and     p1.uid = u.uid)
  22902.     union
  22903.     select    /*    Add rows for table owner */
  22904.         GRANTOR        = user_name(u.uid),
  22905.         GRANTEE        = user_name(o.uid),
  22906.         TABLE_CATALOG    = db_name(),
  22907.         TABLE_SCHEMA    = user_name(o.uid),
  22908.         TABLE_NAME    = o.name,
  22909.         PRIVILEGE_TYPE    = convert(varchar(30),
  22910.                     case v.number
  22911.                     when 193 then 'SELECT'
  22912.                     when 195 then 'INSERT'
  22913.                     when 196 then 'DELETE'
  22914.                     when 197 then 'UPDATE'
  22915.                     else 'REFERENCES'
  22916.                     end),
  22917.         IS_GRANTABLE    = convert(bit,1)    
  22918.     from 
  22919.         sysobjects o, master.dbo.spt_values v, sysusers u
  22920.     where
  22921.         o.type in ('U','V','S')
  22922.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22923.     and    (@grantee is null or @grantee = user_name(o.uid))
  22924.     and     u.uid = 1        /* grantor is dbo of database */
  22925.     and    (@grantor is null or @grantor = user_name(u.uid))
  22926.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  22927.     and     v.number in (26,193,195,196,197)
  22928.     and     not exists (    /* exclude revoke'd privileges */
  22929.             select     *
  22930.             from     sysprotects p1
  22931.             where    p1.protecttype = 206
  22932.             and     p1.action = v.number
  22933.             and     p1.id = o.id
  22934.             and     p1.uid = o.uid)
  22935.     order by 4,5,6,1,2
  22936.     END
  22937. go
  22938. dump tran master with no_log
  22939. go
  22940. CREATE PROCEDURE sp_table_privileges_rowset;2
  22941.     (
  22942.        @handle        int output,
  22943.        @scrollopt    int output,
  22944.     @ccopt        int output,
  22945.     @rows        int output,
  22946.     @table_name     varchar(255) = null,
  22947.     @table_schema    varchar(255) = null,
  22948.     @grantor    varchar(255) = null,
  22949.     @grantee    varchar(255) = null
  22950.     )
  22951. as
  22952. declare @ret int
  22953. SET NOCOUNT ON
  22954. create table #sptprivsrowset1
  22955.     (
  22956.     GRANTOR        sysname not null,
  22957.     GRANTEE        sysname not null,
  22958.     TABLE_CATALOG    sysname not null,
  22959.     TABLE_SCHEMA    sysname not null,
  22960.     TABLE_NAME    sysname not null,
  22961.     PRIVILEGE_TYPE    sysname not null,
  22962.     IS_GRANTABLE    bit not null
  22963.     )
  22964.     
  22965. IF @table_name is not null
  22966.     BEGIN
  22967.     insert into #sptprivsrowset1
  22968.     select
  22969.         GRANTOR        = user_name(p.grantor),
  22970.         GRANTEE        = user_name(u.uid),
  22971.         TABLE_CATALOG    = db_name(),
  22972.         TABLE_SCHEMA    = user_name(o.uid),
  22973.         TABLE_NAME    = o.name,
  22974.         PRIVILEGE_TYPE    = convert(varchar(30),
  22975.                     case p.action
  22976.                     when 193 then 'SELECT'
  22977.                     when 195 then 'INSERT'
  22978.                     when 196 then 'DELETE'
  22979.                     when 197 then 'UPDATE'
  22980.                     else 'REFERENCES'
  22981.                     end),
  22982.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  22983.     from 
  22984.         sysprotects p, sysobjects o, sysusers u
  22985.     where
  22986.         o.name = @table_name
  22987.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  22988.     and    (@grantee is null or @grantee = user_name(u.uid))
  22989.     and     o.type in ('U','V','S')
  22990.     and    p.id = o.id
  22991.     and     (@grantor is null or @grantor = user_name(p.grantor))
  22992.             /* expand groups */
  22993.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  22994.     and     p.protecttype <> 206    /* only grant rows */
  22995.     and     p.action in (26,193,195,196,197)
  22996.     and     o.uid <> u.uid            /* no rows for owner */
  22997.     and     not exists (            /* exclude revoke'd privileges */
  22998.             select     *
  22999.             from     sysprotects p1
  23000.             where    p1.protecttype = 206
  23001.             and     p1.action = p.action
  23002.             and     p1.id = p.id
  23003.             and     p1.uid = u.uid)
  23004.     union
  23005.     select    /*    Add rows for table owner */
  23006.         GRANTOR        = user_name(u.uid),
  23007.         GRANTEE        = user_name(o.uid),
  23008.         TABLE_CATALOG    = db_name(),
  23009.         TABLE_SCHEMA    = user_name(o.uid),
  23010.         TABLE_NAME    = o.name,
  23011.         PRIVILEGE_TYPE    = convert(varchar(30),
  23012.                     case v.number
  23013.                     when 193 then 'SELECT'
  23014.                     when 195 then 'INSERT'
  23015.                     when 196 then 'DELETE'
  23016.                     when 197 then 'UPDATE'
  23017.                     else 'REFERENCES'
  23018.                     end),
  23019.         IS_GRANTABLE    = convert(bit,1)    
  23020.     from 
  23021.         sysobjects o, master.dbo.spt_values v, sysusers u
  23022.     where
  23023.         o.name = @table_name
  23024.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  23025.     and    (@grantee is null or @grantee = user_name(o.uid))
  23026.     and     o.type in ('U','V','S')
  23027.     and     u.uid = 1        /* grantor is dbo of database */
  23028.     and    (@grantor is null or @grantor = user_name(u.uid))
  23029.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23030.     and     v.number in (26,193,195,196,197)
  23031.     and     not exists (    /* exclude revoke'd privileges */
  23032.             select     *
  23033.             from     sysprotects p1
  23034.             where    p1.protecttype = 206
  23035.             and     p1.action = v.number
  23036.             and     p1.id = o.id
  23037.             and     p1.uid = o.uid)
  23038.     order by 4,5,6,1,2
  23039.     END
  23040. ELSE
  23041.     BEGIN
  23042.     insert into #sptprivsrowset1
  23043.     select
  23044.         GRANTOR        = user_name(p.grantor),
  23045.         GRANTEE        = user_name(u.uid),
  23046.         TABLE_CATALOG    = db_name(),
  23047.         TABLE_SCHEMA    = user_name(o.uid),
  23048.         TABLE_NAME    = o.name,
  23049.         PRIVILEGE_TYPE    = convert(varchar(30),
  23050.                     case p.action
  23051.                     when 193 then 'SELECT'
  23052.                     when 195 then 'INSERT'
  23053.                     when 196 then 'DELETE'
  23054.                     when 197 then 'UPDATE'
  23055.                     else 'REFERENCES'
  23056.                     end),
  23057.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23058.     from 
  23059.         sysprotects p, sysobjects o, sysusers u
  23060.     where
  23061.         o.type in ('U','V','S')
  23062.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  23063.     and    (@grantee is null or @grantee = user_name(u.uid))
  23064.     and    p.id = o.id
  23065.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23066.             /* expand groups */
  23067.     and     ((p.uid = u.uid and u.uid <> u.gid) or (p.uid = u.gid and u.uid <> u.gid))
  23068.     and     p.protecttype <> 206    /* only grant rows */
  23069.     and     p.action in (26,193,195,196,197)
  23070.     and     o.uid <> u.uid            /* no rows for owner */
  23071.     and     not exists (            /* exclude revoke'd privileges */
  23072.             select     *
  23073.             from     sysprotects p1
  23074.             where    p1.protecttype = 206
  23075.             and     p1.action = p.action
  23076.             and     p1.id = p.id
  23077.             and     p1.uid = u.uid)
  23078.     union
  23079.     select    /*    Add rows for table owner */
  23080.         GRANTOR        = user_name(u.uid),
  23081.         GRANTEE        = user_name(o.uid),
  23082.         TABLE_CATALOG    = db_name(),
  23083.         TABLE_SCHEMA    = user_name(o.uid),
  23084.         TABLE_NAME    = o.name,
  23085.         PRIVILEGE_TYPE    = convert(varchar(30),
  23086.                     case v.number
  23087.                     when 193 then 'SELECT'
  23088.                     when 195 then 'INSERT'
  23089.                     when 196 then 'DELETE'
  23090.                     when 197 then 'UPDATE'
  23091.                     else 'REFERENCES'
  23092.                     end),
  23093.         IS_GRANTABLE    = convert(bit,1)    
  23094.     from 
  23095.         sysobjects o, master.dbo.spt_values v, sysusers u
  23096.     where
  23097.         o.type in ('U','V','S')
  23098.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  23099.     and    (@grantee is null or @grantee = user_name(o.uid))
  23100.     and     u.uid = 1        /* grantor is dbo of database */
  23101.     and    (@grantor is null or @grantor = user_name(u.uid))
  23102.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23103.     and     v.number in (26,193,195,196,197)
  23104.     and     not exists (    /* exclude revoke'd privileges */
  23105.             select     *
  23106.             from     sysprotects p1
  23107.             where    p1.protecttype = 206
  23108.             and     p1.action = v.number
  23109.             and     p1.id = o.id
  23110.             and     p1.uid = o.uid)
  23111.     order by 4,5,6,1,2
  23112.     END
  23113.  
  23114. exec @ret = sp_cursoropen @handle output, 'select * from #sptprivsrowset1',
  23115.     @scrollopt output, @ccopt output, @rows output
  23116.  
  23117. drop table #sptprivsrowset1
  23118. return isnull(@ret,0)
  23119. go
  23120. dump tran master with no_log
  23121. go
  23122. CREATE PROCEDURE sp_table_privileges_rowset;3
  23123. as
  23124.     select
  23125.         GRANTOR        = convert(sysname, ' '),
  23126.         GRANTEE        = convert(sysname, ' '),
  23127.         TABLE_CATALOG    = convert(sysname, ' '),
  23128.         TABLE_SCHEMA    = convert(sysname, ' '),
  23129.         TABLE_NAME    = convert(sysname, ' '),
  23130.         PRIVILEGE_TYPE    = convert(varchar(30), ' '),
  23131.         IS_GRANTABLE    = convert(bit, 0)
  23132.     where    1=0
  23133. go
  23134.  
  23135. if (charindex('7.00', @@version) = 0 and
  23136.     charindex('8.00', @@version) = 0)
  23137. begin
  23138.     print ''
  23139.     print ''
  23140.     print 'Warning:'
  23141.     print 'you are installing the stored procedures '
  23142.     print 'on a pre 8.0 SQL Server.'
  23143.     print 'Ignore the following errors.'
  23144. end
  23145. else
  23146.     drop proc sp_table_privileges_rowset
  23147. go
  23148.  
  23149.  
  23150. /*    Procedure for 8.0 server */
  23151. CREATE PROCEDURE sp_table_privileges_rowset
  23152.     (
  23153.     @table_name         sysname,
  23154.     @table_schema        sysname = null,
  23155.     @grantor        sysname = null,
  23156.     @grantee        sysname = null
  23157.     )
  23158. as
  23159.     select
  23160.         GRANTOR        = user_name(p.grantor),
  23161.         GRANTEE        = user_name(u.uid),
  23162.         TABLE_CATALOG    = db_name(),
  23163.         TABLE_SCHEMA    = user_name(o.uid),
  23164.         TABLE_NAME    = o.name,
  23165.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  23166.                     case p.action
  23167.                     when 193 then N'SELECT'
  23168.                     when 195 then N'INSERT'
  23169.                     when 196 then N'DELETE'
  23170.                     when 197 then N'UPDATE'
  23171.                     else N'REFERENCES'
  23172.                     end),
  23173.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23174.     from 
  23175.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  23176.     where
  23177.         o.name = @table_name
  23178.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  23179.     and    (@grantee is null or @grantee = user_name(u.uid))
  23180.     and     o.type in ('U','V','S')
  23181.     and    p.id = o.id
  23182.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23183.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  23184.     and     (u.uid > 0 and u.uid < 16384)
  23185.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  23186.     and     p.protecttype <> 206    /* only grant rows */
  23187.     and     p.action in (26,193,195,196,197)
  23188.     and     o.uid <> u.uid            /* no rows for owner */
  23189.     and     not exists (            /* exclude revoke'd privileges */
  23190.             select     *
  23191.             from     sysprotects p1
  23192.             where    p1.protecttype = 206
  23193.             and     p1.action = p.action
  23194.             and     p1.id = p.id
  23195.             and     p1.uid = u.uid)
  23196.     union
  23197.     select    /*    Add rows for table owner */
  23198.         GRANTOR        = user_name(u.uid),
  23199.         GRANTEE        = user_name(o.uid),
  23200.         TABLE_CATALOG    = db_name(),
  23201.         TABLE_SCHEMA    = user_name(o.uid),
  23202.         TABLE_NAME    = o.name,
  23203.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  23204.                     case v.number
  23205.                     when 193 then N'SELECT'
  23206.                     when 195 then N'INSERT'
  23207.                     when 196 then N'DELETE'
  23208.                     when 197 then N'UPDATE'
  23209.                     else N'REFERENCES'
  23210.                     end),
  23211.         IS_GRANTABLE    = convert(bit,1)    
  23212.     from 
  23213.         sysobjects o, master.dbo.spt_values v, sysusers u
  23214.     where
  23215.         o.name = @table_name
  23216.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  23217.     and    (@grantee is null or @grantee = user_name(o.uid))
  23218.     and     o.type in ('U','V','S')
  23219.     and     u.uid = 1        /* grantor is dbo of database */
  23220.     and    (@grantor is null or @grantor = user_name(u.uid))
  23221.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23222.     and     v.number in (26,193,195,196,197)
  23223.     and     not exists (    /* exclude revoke'd privileges */
  23224.             select     *
  23225.             from     sysprotects p1
  23226.             where    p1.protecttype = 206
  23227.             and     p1.action = v.number
  23228.             and     p1.id = o.id
  23229.             and     p1.uid = o.uid)
  23230.     order by 4,5,6,1,2
  23231. go
  23232. dump tran master with no_log
  23233. go
  23234. CREATE PROCEDURE sp_table_privileges_rowset;2
  23235.     (
  23236.     @table_schema        sysname = null,
  23237.     @grantor        sysname = null,
  23238.     @grantee        sysname = null
  23239.     )
  23240. as
  23241.     select
  23242.         GRANTOR        = user_name(p.grantor),
  23243.         GRANTEE        = user_name(u.uid),
  23244.         TABLE_CATALOG    = db_name(),
  23245.         TABLE_SCHEMA    = user_name(o.uid),
  23246.         TABLE_NAME    = o.name,
  23247.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  23248.                     case p.action
  23249.                     when 193 then N'SELECT'
  23250.                     when 195 then N'INSERT'
  23251.                     when 196 then N'DELETE'
  23252.                     when 197 then N'UPDATE'
  23253.                     else N'REFERENCES'
  23254.                     end),
  23255.         IS_GRANTABLE    = convert(bit,case when p.protecttype = 205 then 0 else 1 end)
  23256.     from 
  23257.         sysprotects p, sysobjects o, sysusers u, sysmembers m
  23258.     where
  23259.         o.type in ('U','V','S')
  23260.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  23261.     and    (@grantee is null or @grantee = user_name(u.uid))
  23262.     and    p.id = o.id
  23263.     and     (@grantor is null or @grantor = user_name(p.grantor))
  23264.         /* expand groups - AKUNDONE: only 1 level of grp unrolling. */
  23265.     and     (u.uid > 0 and u.uid < 16384)
  23266.     and     ((p.uid = u.uid) or (p.uid = m.groupuid and u.uid = m.memberuid))
  23267.     and     p.protecttype <> 206    /* only grant rows */
  23268.     and     p.action in (26,193,195,196,197)
  23269.     and     o.uid <> u.uid            /* no rows for owner */
  23270.     and     not exists (            /* exclude revoke'd privileges */
  23271.             select     *
  23272.             from     sysprotects p1
  23273.             where    p1.protecttype = 206
  23274.             and     p1.action = p.action
  23275.             and     p1.id = p.id
  23276.             and     p1.uid = u.uid)
  23277.     union
  23278.     select    /*    Add rows for table owner */
  23279.         GRANTOR        = user_name(u.uid),
  23280.         GRANTEE        = user_name(o.uid),
  23281.         TABLE_CATALOG    = db_name(),
  23282.         TABLE_SCHEMA    = user_name(o.uid),
  23283.         TABLE_NAME    = o.name,
  23284.         PRIVILEGE_TYPE    = convert(nvarchar(30),
  23285.                     case v.number
  23286.                     when 193 then N'SELECT'
  23287.                     when 195 then N'INSERT'
  23288.                     when 196 then N'DELETE'
  23289.                     when 197 then N'UPDATE'
  23290.                     else N'REFERENCES'
  23291.                     end),
  23292.         IS_GRANTABLE    = convert(bit,1)    
  23293.     from 
  23294.         sysobjects o, master.dbo.spt_values v, sysusers u
  23295.     where
  23296.         o.type in ('U','V','S')
  23297.     and    (@table_schema is null or @table_schema = user_name(o.uid))
  23298.     and    (@grantee is null or @grantee = user_name(o.uid))
  23299.     and     u.uid = 1        /* grantor is dbo of database */
  23300.     and    (@grantor is null or @grantor = user_name(u.uid))
  23301.     and     v.type = 'P'    /* cross product to get all exposed privileges */
  23302.     and     v.number in (26,193,195,196,197)
  23303.     and     not exists (    /* exclude revoke'd privileges */
  23304.             select     *
  23305.             from     sysprotects p1
  23306.             where    p1.protecttype = 206
  23307.             and     p1.action = v.number
  23308.             and     p1.id = o.id
  23309.             and     p1.uid = o.uid)
  23310.     order by 4,5,6,1,2
  23311. go
  23312. dump tran master with no_log
  23313. go
  23314. create procedure sp_table_privileges_rowset;5
  23315.     (
  23316.     @table_server        sysname,
  23317.     @table_catalog        sysname = null,
  23318.     @table_name        sysname = null,
  23319.     @table_schema        sysname = null,
  23320.     @grantor        sysname = null,
  23321.     @grantee        sysname = null
  23322.     )
  23323. as
  23324.     select
  23325.         GRANTOR,        
  23326.         GRANTEE,        
  23327.         TABLE_CATALOG,    
  23328.         TABLE_SCHEMA,    
  23329.         TABLE_NAME,    
  23330.         PRIVILEGE_TYPE,    
  23331.         IS_GRANTABLE    
  23332.     from master.dbo.SYSREMOTE_TABLE_PRIVILEGES <
  23333.                 @table_server,
  23334.                 @table_catalog,
  23335.                 @table_schema,
  23336.                 @table_name,
  23337.                 @grantor,
  23338.                 @grantee >
  23339.     order by 3,4,5,6,1,2
  23340. go
  23341.  
  23342. grant execute on sp_table_privileges_rowset to public
  23343. go
  23344. dump tran master with no_log
  23345. go
  23346. if (charindex('6.00', @@version) > 0)
  23347.     begin
  23348.     if (exists (select * from sysobjects
  23349.         where name = 'sp_table_privileges_rowset' and type = 'P '))
  23350.         begin
  23351.         drop proc sp_table_privileges_rowset
  23352.         dump tran master with no_log
  23353.         end
  23354.     end
  23355. go
  23356.  
  23357.  
  23358. print ''
  23359. print 'creating sp_linkedservers_rowset'
  23360. go
  23361. if (charindex('7.00', @@version) = 0 and
  23362.     charindex('8.00', @@version) = 0)
  23363. begin
  23364.     print ''
  23365.     print ''
  23366.     print 'Warning:'
  23367.     print 'you are installing the stored procedures '
  23368.     print 'on a pre 8.0 SQL Server.'
  23369.     print 'Ignore the following errors.'
  23370. end
  23371. go
  23372. /*    Procedure for 8.0 server */
  23373. create proc sp_linkedservers_rowset
  23374.     (
  23375.     @srvname     sysname
  23376.     )
  23377. as
  23378.     select
  23379.         SVR_NAME         = srvname,
  23380.         SVR_PRODUCT        = srvproduct,
  23381.         SVR_PROVIDERNAME    = providername,
  23382.         SVR_DATASOURCE        = datasource,
  23383.         SVR_PROVIDERSTRING    = providerstring,
  23384.         SVR_LOCATION        = location,
  23385.         SVR_CATALOG        = catalog
  23386.     from master.dbo.sysservers
  23387.     where srvname = @srvname and (srvstatus & 128) = 128
  23388.     order by 1
  23389. go
  23390. dump tran master with no_log
  23391. go
  23392. create proc sp_linkedservers_rowset;2
  23393. as
  23394.     select
  23395.         SVR_NAME         = srvname,
  23396.         SVR_PRODUCT        = srvproduct,
  23397.         SVR_PROVIDERNAME    = providername,
  23398.         SVR_DATASOURCE        = datasource,
  23399.         SVR_PROVIDERSTRING    = providerstring,
  23400.         SVR_LOCATION        = location,
  23401.         SVR_CATALOG        = catalog
  23402.     from master.dbo.sysservers
  23403.     where (srvstatus & 128) = 128
  23404.     order by 1
  23405. go
  23406.  
  23407. grant execute on sp_linkedservers_rowset to public
  23408. go
  23409.  
  23410. dump tran master with no_log
  23411. go
  23412.  
  23413. print ''
  23414. print 'creating sp_table_statistics_rowset'
  23415. go
  23416. if (charindex('8.00', @@version) = 0)
  23417. begin
  23418.     print ''
  23419.     print ''
  23420.     print 'Warning:'
  23421.     print 'you are installing the stored procedures '
  23422.     print 'on a pre 8.00 SQL Server.'
  23423.     print 'Ignore the following errors.'
  23424. end
  23425. go
  23426. /*    Procedure for 8.00 server */
  23427. create proc sp_table_statistics_rowset
  23428. as
  23429.     select
  23430.         TABLE_CATALOG = convert(sysname, null),
  23431.         TABLE_SCHEMA = convert(sysname, null),
  23432.         TABLE_NAME = convert(sysname, null),
  23433.         STATISTICS_CATALOG = convert(sysname, null),
  23434.         STATISTICS_SCHEMA = convert(sysname, null),
  23435.         STATISTICS_NAME = convert(sysname, null),
  23436.         STATISTICS_TYPE = convert(smallint,0),
  23437.         COLUMN_NAME = convert(sysname, null),
  23438.         COLUMN_GUID = convert(uniqueidentifier, null),
  23439.         COLUMN_PROPID = convert(int, null),
  23440.         ORDINAL_POSITION = convert(int, null),
  23441.         SAMPLE_PCT = convert(smallint, null),
  23442.         LAST_UPDATE_TIME = convert(datetime, null),
  23443.         NO_OF_RANGES = convert(int, null),
  23444.         COLUMN_CARDINALITY = convert(bigint, null),
  23445.         TUPLE_CARDINALITY = convert(bigint, null),
  23446.         TABLE_CARDINALITY = convert(bigint, null),
  23447.         AVG_COLUMN_LENGTH = convert(int, null)
  23448.     where 1=0
  23449. go
  23450. dump tran master with no_log
  23451. go
  23452.  
  23453. create proc sp_table_statistics_rowset;2
  23454.                 (@table_catalog        sysname = null,
  23455.                 @table_schema        sysname = null,
  23456.                 @table_name        sysname = null,
  23457.                 @stat_catalog        sysname = null,
  23458.                 @stat_schema        sysname = null,
  23459.                 @stat_name        sysname = null)
  23460. as 
  23461. begin
  23462.     
  23463.     set nocount on
  23464.  
  23465.     -- check in parameters
  23466.     if ((@table_catalog is not null) and (db_name() <> @table_catalog)
  23467.         or (@stat_catalog is not null) and (db_name() <> @stat_catalog))
  23468.         begin    /* If qualifier doesn't match current database */
  23469.             raiserror 20001 '~~Rush_5~~'
  23470.             return
  23471.         end
  23472.  
  23473.     --- create temp table
  23474.     create table #spstattab(
  23475.         tblcatalog    sysname collate database_default not null,
  23476.         tblschema    sysname collate database_default not null,
  23477.         tblname        sysname collate database_default not null,
  23478.         statname    sysname collate database_default not null
  23479.         )
  23480.  
  23481.     insert into #spstattab
  23482.     select 
  23483.         db_name(),
  23484.         user_name(o.uid),
  23485.         o.name,
  23486.         x.name
  23487.     from sysobjects o, sysindexes x
  23488.     where o.type in ('U')
  23489.         and (@table_name is null or o.name = @table_name)
  23490.         and (@table_schema is null or @table_schema = user_name(o.uid))
  23491.         and (@stat_name is null or @stat_name = x.name)
  23492.         and (@stat_schema is null or @stat_schema = user_name(o.uid))
  23493.         and x.id = o.id
  23494.         and permissions(o.id) <> 0
  23495.         and x.statblob is not null
  23496.     order by 1,2,3,4
  23497.  
  23498.     -- fast forward CURSOR OVER THE temp TABLE
  23499.     declare ff_csr  cursor fast_forward for
  23500.     select tblcatalog, tblschema, tblname, statname from #spstattab
  23501.  
  23502.     declare @tblcatalog    sysname,
  23503.         @tblschema    sysname,
  23504.         @tblname    sysname,
  23505.         @statname    sysname,
  23506.         @qtbl        nvarchar(4000),
  23507.         @rowsetcount    int
  23508.  
  23509.     set @rowsetcount = 0
  23510.  
  23511.     open ff_csr
  23512.     fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23513.     while @@fetch_status >= 0
  23514.     begin
  23515.         set @rowsetcount = @rowsetcount + 1
  23516.         set @qtbl = @tblcatalog + '.' + @tblschema + '.' + @tblname
  23517.         dbcc show_statistics(@qtbl, @statname) with stat_header join density_vector
  23518.         fetch ff_csr into @tblcatalog, @tblschema, @tblname, @statname
  23519.     end
  23520.     close ff_csr
  23521.     deallocate ff_csr
  23522.     
  23523.     --- drop temp table
  23524.     drop table #spstattab
  23525.  
  23526.     if @rowsetcount = 0
  23527.     begin
  23528.         select    
  23529.             TABLE_CATALOG = convert(sysname, null),
  23530.             TABLE_SCHEMA = convert(sysname, null),
  23531.             TABLE_NAME = convert(sysname, null),
  23532.             STATISTICS_CATALOG = convert(sysname, null),
  23533.             STATISTICS_SCHEMA = convert(sysname, null),
  23534.             STATISTICS_NAME = convert(sysname, null),
  23535.             STATISTICS_TYPE = convert(smallint,0),
  23536.             COLUMN_NAME = convert(sysname, null),
  23537.             COLUMN_GUID = convert(uniqueidentifier, null),
  23538.             COLUMN_PROPID = convert(int, null),
  23539.             ORDINAL_POSITION = convert(int, null),
  23540.             SAMPLE_PCT = convert(smallint, null),
  23541.             LAST_UPDATE_TIME = convert(datetime, null),
  23542.             NO_OF_RANGES = convert(int, null),
  23543.             COLUMN_CARDINALITY = convert(bigint, null),
  23544.             TUPLE_CARDINALITY = convert(bigint, null),
  23545.             TABLE_CARDINALITY = convert(bigint, null),
  23546.             AVG_COLUMN_LENGTH = convert(int, null)
  23547.         where 1=0
  23548.     end
  23549.  
  23550.     set nocount off
  23551. end
  23552. go
  23553.  
  23554. grant execute on sp_table_statistics_rowset to public
  23555. go
  23556.  
  23557. dump tran master with no_log
  23558. go
  23559.  
  23560. print ''
  23561. print 'creating sp_oledb_column_constraints'
  23562. go
  23563.  
  23564. /* Procedure for 6.5 server */
  23565. create procedure sp_oledb_column_constraints
  23566.     (@tblname varchar(225)        -- the table to check for constraints
  23567.     ,@colname varchar(225)        -- the column to check for constraints
  23568.     ,@dropcnst smallint)
  23569. as
  23570. begin
  23571.     -- PRELIM
  23572.     set nocount on
  23573.  
  23574.     declare    @tblid        int           -- the object id of the table
  23575.         ,@cnstname        varchar(225)   -- name of const. currently under consideration
  23576.         ,@cnstid        int
  23577.         ,@cnstatus        int
  23578.         ,@dbname        varchar(30)
  23579.  
  23580.     -- Create temp table
  23581.     create table #spcnsttab
  23582.     (cnst_name        varchar(225)        NOT NULL)
  23583.  
  23584.     -- Check to see that the object names are local to the current database.
  23585.     if @tblname like '%.%.%'
  23586.           and  substring(@tblname, 1, charindex('.', @tblname) - 1) <> db_name()
  23587.        begin
  23588.            raiserror(15250,-1,-1)
  23589.            return (1)
  23590.        end
  23591.     -- Check to see if the table exists and initialize @objid.
  23592.     select @tblid = object_id(@tblname)
  23593.     if @tblid is NULL
  23594.     begin
  23595.         select @dbname=db_name()
  23596.         raiserror(15009,-1,-1,@tblname,@dbname)
  23597.         return (1)
  23598.     end
  23599.  
  23600.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23601.     declare cnst_csr insensitive cursor for
  23602.         select c.constid, c.status, o.name 
  23603.         from sysconstraints c, sysobjects o 
  23604.         where c.id = @tblid and o.id = c.constid
  23605.             and ((c.status & 0xf)=1 and (@dropcnst & 16)<> 0
  23606.             or (c.status & 0xf)=2 and (@dropcnst & 32)<> 0 
  23607.             or (c.status & 0xf)=5 and (@dropcnst & 2)<> 0)    
  23608.         -- ONLY 6.5 sysconstraints objects
  23609.         for read only
  23610.  
  23611.     -- Now check out each constraint, figure out its type and keys and
  23612.     -- save the info in a temporary table that we'll print out at the end.
  23613.     open cnst_csr
  23614.     fetch cnst_csr into @cnstid, @cnstatus, @cnstname
  23615.     while @@fetch_status >= 0
  23616.     begin
  23617.         if ((@cnstatus & 0xf) in (1, 2)) ---- primary key, unique
  23618.             begin
  23619.                 -- get indid
  23620.                 declare @indid smallint
  23621.                 select    @indid = indid
  23622.                 from    sysindexes
  23623.                 where    name = object_name(@cnstid) and id = @tblid
  23624.  
  23625.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23626.                     and (index_col(@tblname, @indid, 2) is null)
  23627.                     begin
  23628.                         -- ADD TO TABLE
  23629.                         insert into #spcnsttab
  23630.                         values (@cnstname)
  23631.                     end
  23632.             end
  23633.         else
  23634.         if (@cnstatus & 0xf) = 5
  23635.             begin
  23636.                 if (select col_name(id, colid) from sysconstraints where colid > 0 and constid=@cnstid) = @colname
  23637.                     begin
  23638.                         insert into #spcnsttab
  23639.                         values (@cnstname)
  23640.                     end
  23641.             end
  23642.  
  23643.         fetch cnst_csr into @cnstid ,@cnstatus ,@cnstname
  23644.     end        --of major loop
  23645.     close cnst_csr
  23646.     deallocate cnst_csr
  23647.  
  23648.     -- Now print out the contents of the temporary index table.
  23649.     select 'constraint_name' = cnst_name
  23650.     from #spcnsttab 
  23651.     
  23652.     drop table #spcnsttab
  23653.  
  23654.     set nocount off
  23655.     return (0)
  23656. end    
  23657. go
  23658. dump tran master with no_log
  23659. go
  23660.  
  23661. if (charindex('7.00', @@version) = 0 and
  23662.     charindex('8.00', @@version) = 0)
  23663. begin
  23664.     print ''
  23665.     print ''
  23666.     print 'Warning:'
  23667.     print 'you are installing the stored procedures '
  23668.     print 'on a pre 7.0 SQL Server.'
  23669.     print 'Ignore the following errors.'
  23670. end
  23671. else
  23672.     drop proc sp_oledb_column_constraints
  23673. go
  23674.  
  23675. /* Procedure for 7.0 server */
  23676. create procedure sp_oledb_column_constraints
  23677.     (@tblname nvarchar(776)        -- the table to check for constraints
  23678.     ,@colname nvarchar(776)        -- the column to check for constraints
  23679.     ,@dropcnst smallint)
  23680. as
  23681. begin
  23682.     -- PRELIM
  23683.     set nocount on
  23684.  
  23685.     declare    @tblid        int           -- the object id of the table
  23686.         ,@cnstname        sysname       -- name of const. currently under consideration
  23687.         ,@cnstid        int
  23688.         ,@cnsttype        character(2)
  23689.         ,@dbname        sysname
  23690.  
  23691.     -- Create temp table
  23692.     create table #spcnsttab
  23693.     (cnst_name        sysname        NOT NULL)
  23694.  
  23695.     -- Check to see that the object names are local to the current database.
  23696.     select @dbname = parsename(@tblname,3) 
  23697.     if @dbname is not null and @dbname <> db_name()
  23698.     begin
  23699.         raiserror(15250,-1,-1)
  23700.         return (1)
  23701.     end
  23702.  
  23703.     -- Check to see if the table exists and initialize @objid.
  23704.     select @tblid = object_id(@tblname)
  23705.     if @tblid is NULL
  23706.     begin
  23707.         select @dbname=db_name()
  23708.         raiserror(15009,-1,-1,@tblname,@dbname)
  23709.         return (1)
  23710.     end
  23711.  
  23712.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23713.     declare cnst_csr cursor fast_forward for
  23714.         select id, xtype, name 
  23715.         from sysobjects 
  23716.         where parent_obj = @tblid
  23717.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  23718.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  23719.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))    
  23720.         -- ONLY 6.5 sysconstraints objects
  23721.         for read only
  23722.  
  23723.     -- Now check out each constraint, figure out its type and keys and
  23724.     -- save the info in a temporary table that we'll print out at the end.
  23725.     open cnst_csr
  23726.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  23727.     while @@fetch_status >= 0
  23728.     begin
  23729.         if @cnsttype in ('PK','UQ')
  23730.             begin
  23731.                 -- get indid
  23732.                 declare @indid smallint
  23733.                 select    @indid = indid
  23734.                 from    sysindexes
  23735.                 where    name = object_name(@cnstid) and id = @tblid
  23736.  
  23737.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23738.                     and (index_col(@tblname, @indid, 2) is null)
  23739.                     begin
  23740.                         -- ADD TO TABLE
  23741.                         insert into #spcnsttab
  23742.                         values (@cnstname)
  23743.                     end
  23744.             end
  23745.         else
  23746.         if (@cnsttype = 'D ')
  23747.             begin
  23748.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  23749.                     begin
  23750.                         insert into #spcnsttab
  23751.                         values (@cnstname)
  23752.                     end
  23753.             end
  23754.  
  23755.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  23756.     end        --of major loop
  23757.     close cnst_csr
  23758.     deallocate cnst_csr
  23759.  
  23760.     -- Now print out the contents of the temporary index table.
  23761.     select 'constraint_name' = cnst_name
  23762.     from #spcnsttab 
  23763.  
  23764.     drop table #spcnsttab
  23765.     
  23766.     set nocount off
  23767.     return (0)
  23768. end
  23769. go
  23770.  
  23771. if (charindex('8.00', @@version) = 0)
  23772. begin
  23773.     print ''
  23774.     print ''
  23775.     print 'Warning:'
  23776.     print 'you are installing the stored procedures '
  23777.     print 'on a pre 8.0 SQL Server.'
  23778.     print 'Ignore the following errors.'
  23779. end
  23780. else
  23781.     drop proc sp_oledb_column_constraints
  23782. go
  23783.  
  23784. /* Procedure for 8.0 server */
  23785. create procedure sp_oledb_column_constraints
  23786.     (@tblname nvarchar(776)        -- the table to check for constraints
  23787.     ,@colname nvarchar(776)        -- the column to check for constraints
  23788.     ,@dropcnst smallint)
  23789. as
  23790. begin
  23791.     -- PRELIM
  23792.     set nocount on
  23793.  
  23794.     declare    @tblid        int           -- the object id of the table
  23795.         ,@cnstname        sysname       -- name of const. currently under consideration
  23796.         ,@cnstid        int
  23797.         ,@cnsttype        character(2)
  23798.         ,@dbname        sysname
  23799.  
  23800.     -- Create temp table
  23801.     create table #spcnsttab
  23802.     (cnst_name        sysname        collate database_default NOT NULL)
  23803.  
  23804.     -- Check to see that the object names are local to the current database.
  23805.     select @dbname = parsename(@tblname,3) 
  23806.     if @dbname is not null and @dbname <> db_name()
  23807.     begin
  23808.         raiserror(15250,-1,-1)
  23809.         return (1)
  23810.     end
  23811.  
  23812.     -- Check to see if the table exists and initialize @objid.
  23813.     select @tblid = object_id(@tblname)
  23814.     if @tblid is NULL
  23815.     begin
  23816.         select @dbname=db_name()
  23817.         raiserror(15009,-1,-1,@tblname,@dbname)
  23818.         return (1)
  23819.     end
  23820.  
  23821.     -- STATIC CURSOR OVER THE TABLE'S CONSTRAINTS
  23822.     declare cnst_csr cursor fast_forward for
  23823.         select id, xtype, name 
  23824.         from sysobjects 
  23825.         where parent_obj = @tblid
  23826.             and ((xtype = 'PK' and (@dropcnst & 16)<> 0) 
  23827.             or (xtype = 'UQ' and (@dropcnst & 32)<> 0)
  23828.             or (xtype = 'D ' and (@dropcnst & 2)<> 0))    
  23829.         -- ONLY 6.5 sysconstraints objects
  23830.         for read only
  23831.  
  23832.     -- Now check out each constraint, figure out its type and keys and
  23833.     -- save the info in a temporary table that we'll print out at the end.
  23834.     open cnst_csr
  23835.     fetch cnst_csr into @cnstid, @cnsttype, @cnstname
  23836.     while @@fetch_status >= 0
  23837.     begin
  23838.         if @cnsttype in ('PK','UQ')
  23839.             begin
  23840.                 -- get indid
  23841.                 declare @indid smallint
  23842.                 select    @indid = indid
  23843.                 from    sysindexes
  23844.                 where    name = object_name(@cnstid) and id = @tblid
  23845.  
  23846.                 if (@colname = index_col(@tblname, @indid, 1)) 
  23847.                     and (index_col(@tblname, @indid, 2) is null)
  23848.                     begin
  23849.                         -- ADD TO TABLE
  23850.                         insert into #spcnsttab
  23851.                         values (@cnstname)
  23852.                     end
  23853.             end
  23854.         else
  23855.         if (@cnsttype = 'D ')
  23856.             begin
  23857.                 if (select col_name(@tblid, info) from sysobjects where id=@cnstid) = @colname
  23858.                     begin
  23859.                         insert into #spcnsttab
  23860.                         values (@cnstname)
  23861.                     end
  23862.             end
  23863.  
  23864.         fetch cnst_csr into @cnstid ,@cnsttype ,@cnstname
  23865.     end        --of major loop
  23866.     close cnst_csr
  23867.     deallocate cnst_csr
  23868.  
  23869.     -- Now print out the contents of the temporary index table.
  23870.     select 'constraint_name' = cnst_name
  23871.     from #spcnsttab 
  23872.  
  23873.     drop table #spcnsttab
  23874.     
  23875.     set nocount off
  23876.     return (0)
  23877. end
  23878. go
  23879.  
  23880. grant execute on sp_oledb_column_constraints to public
  23881. go
  23882.  
  23883. dump tran master with no_log
  23884. go
  23885.  
  23886. print ''
  23887. print 'creating sp_oledb_indexinfo'
  23888. go
  23889.  
  23890. /* Procedure for 6.5 server */
  23891. create procedure sp_oledb_indexinfo
  23892.         @objname varchar(225)        -- the table to check for indexes
  23893.         ,@indname varchar(225)
  23894. as
  23895. begin
  23896.     -- PRELIM
  23897.     set nocount on
  23898.  
  23899.     declare @objid int,            -- the object id of the table
  23900.         @indid smallint,    -- the index id of an index
  23901.         @status int,
  23902.         @dbname    varchar(30),
  23903.         @OrigFillFactor tinyint,
  23904.         @i int, 
  23905.         @thiskey varchar(32),
  23906.         @tptr     varbinary(16) -- pointer for building text strings.
  23907.     ---- Check to see that the object names are local to the current database.
  23908.     if @objname like '%.%.%'
  23909.           and  substring(@objname, 1, charindex('.', @objname) - 1) <> db_name()
  23910.        begin
  23911.            raiserror(15250,-1,-1)
  23912.            return (1)
  23913.        end
  23914.     ---- Check to see if the table exists and initialize @objid.
  23915.     select @objid = object_id(@objname)
  23916.     ---- Table does not exist so return.
  23917.     if @objid is NULL
  23918.        begin
  23919.            select @dbname=db_name()
  23920.            raiserror(15009,-1,-1,@objname,@dbname)
  23921.            return (1)
  23922.        end
  23923.  
  23924.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  23925.     from sysindexes
  23926.     where id = @objid and name = @indname 
  23927.  
  23928.     -- IF NO INDEX, QUIT
  23929.     if @indid is NULL
  23930.     begin
  23931.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  23932.         return (1)
  23933.     end
  23934.     -- create temp table
  23935.     create table #spindtab
  23936.     (
  23937.         status                int,
  23938.         OrigFillFactor            tinyint,
  23939.         index_keys            text    NOT NULL,
  23940.     )
  23941.     -- Now check out each index, figure out its type and keys and
  23942.     -- First we'll figure out what the keys are.
  23943.     select @i = 1
  23944.     while (@i <= 16)
  23945.         begin
  23946.             select @thiskey = index_col(@objname, @indid, @i)
  23947.              if @thiskey is NULL
  23948.                     goto keysdone
  23949.              if @i=1
  23950.                 begin
  23951.                     insert into #spindtab
  23952.                     values (@status, @OrigFillFactor, @thiskey)
  23953.  
  23954.             select @tptr = textptr(index_keys) from #spindtab
  23955.                 end
  23956.              else
  23957.                 begin
  23958.                     select @thiskey = ', ' + @thiskey
  23959.  
  23960.             if @tptr is not null
  23961.                        updatetext #spindtab.index_keys @tptr null null @thiskey
  23962.                 end
  23963.  
  23964.         select @i = @i + 1
  23965.          end --loop 16
  23966.          ---- When we get here we now have all the keys.
  23967.  
  23968. keysdone:
  23969.     select * from #spindtab
  23970.     
  23971.     drop table #spindtab
  23972.     set nocount off
  23973.     return (0)
  23974. end
  23975. go
  23976. dump tran master with no_log
  23977. go
  23978.  
  23979. if (charindex('7.00', @@version) = 0 and
  23980.     charindex('8.00', @@version) = 0)
  23981. begin
  23982.     print ''
  23983.     print ''
  23984.     print 'Warning:'
  23985.     print 'you are installing the stored procedures '
  23986.     print 'on a pre 8.0 SQL Server.'
  23987.     print 'Ignore the following errors.'
  23988. end
  23989. else
  23990.     drop proc sp_oledb_indexinfo
  23991. go
  23992.  
  23993. /* Procedure for 8.0 server */
  23994. create proc sp_oledb_indexinfo
  23995.         @objname nvarchar(776)        -- the table to check for indexes
  23996.         ,@indname nvarchar(776)
  23997. as
  23998. begin
  23999.     -- PRELIM
  24000.     set nocount on
  24001.  
  24002.     declare @objid int,            -- the object id of the table
  24003.         @indid smallint,    -- the index id of an index
  24004.         @status int,
  24005.         @keys nvarchar(2078),-- string build index key list, length = (16*max_id_length)+(15*2)
  24006.         @dbname    sysname,
  24007.         @OrigFillFactor tinyint
  24008.  
  24009.     -- Check to see that the object names are local to the current database.
  24010.     select @dbname = parsename(@objname,3) 
  24011.  
  24012.     if @dbname is not null and @dbname <> db_name()
  24013.     begin
  24014.             raiserror(15250,-1,-1)
  24015.             return (1)
  24016.     end
  24017.  
  24018.     -- Check to see the the table exists and initialize @objid.
  24019.     select @objid = object_id(@objname)
  24020.     if @objid is NULL
  24021.     begin
  24022.         select @dbname=db_name()
  24023.         raiserror(15009,-1,-1,@objname,@dbname)
  24024.         return (1)
  24025.     end
  24026.  
  24027.     select @indid = indid, @status = status, @OrigFillFactor = OrigFillFactor
  24028.     from sysindexes
  24029.     where id = @objid and name = @indname 
  24030.  
  24031.     -- IF NO INDEX, QUIT
  24032.     if @indid is NULL
  24033.     begin
  24034.         raiserror(15472,-1,-1) --'Object does not have any indexes.'
  24035.         return (1)
  24036.     end
  24037.  
  24038.     -- First we'll figure out what the keys are.
  24039.     declare @i int, @thiskey sysname
  24040.     select @keys = index_col(@objname, @indid, 1),
  24041.         @i = 2, @thiskey = index_col(@objname, @indid, 2)
  24042.     while (@thiskey is not null )
  24043.     begin
  24044.         select @keys = @keys + ', ' + @thiskey, @i = @i + 1
  24045.         select @thiskey = index_col(@objname, @indid, @i)
  24046.     end
  24047.     -- DISPLAY THE RESULTS
  24048.     select 'Status'=@status, 'OrigFillFactor' =@OrigFillFactor, 'Index keys'=@keys
  24049.     set nocount off
  24050.     return (0) 
  24051. end
  24052. go
  24053.  
  24054. grant execute on sp_oledb_indexinfo to public
  24055. go
  24056.  
  24057. dump tran master with no_log
  24058. go
  24059.  
  24060. print ''
  24061. print 'creating sp_oledb_ro_usrname'
  24062. go
  24063.  
  24064. create procedure sp_oledb_ro_usrname
  24065. as
  24066. begin
  24067.     select substring('NY',status/1024&1+1,1),user_name() 
  24068.     from master..sysdatabases 
  24069.     where name=DB_NAME()
  24070. end
  24071. go
  24072.  
  24073. grant execute on sp_oledb_ro_usrname to public
  24074. go
  24075.  
  24076. dump tran master with no_log
  24077. go
  24078.  
  24079. if (charindex('6.00', @@version) > 0 or
  24080.     charindex('6.50', @@version) > 0 or
  24081.     charindex('7.00', @@version) > 0)
  24082.     begin
  24083.     if (exists (select * from sysobjects
  24084.         where name = 'sp_oledb_ro_usrname' and type = 'P '))
  24085.         begin
  24086.             drop proc sp_oledb_ro_usrname
  24087.             dump tran master with no_log
  24088.         end
  24089.     end
  24090. go
  24091.  
  24092. print ''
  24093. print 'creating sp_oledb_deflang'
  24094. go
  24095.  
  24096. if (charindex('8.00', @@version) = 0)
  24097. begin
  24098.     print ''
  24099.     print ''
  24100.     print 'Warning:'
  24101.     print 'you are installing the stored procedures '
  24102.     print 'on a pre 8.0 SQL Server.'
  24103.     print 'Ignore the following errors.'
  24104. end
  24105. go
  24106.  
  24107. create procedure sp_oledb_deflang
  24108. as
  24109.     begin
  24110.         select ISNULL(language,'us_english') 
  24111.         from master..syslogins 
  24112.         where sid=SUSER_SID()
  24113.     end
  24114. go
  24115.  
  24116. grant execute on sp_oledb_deflang to public
  24117. go
  24118.  
  24119. dump tran master with no_log
  24120. go
  24121.  
  24122. if (charindex('6.00', @@version) > 0 or
  24123.     charindex('6.50', @@version) > 0 or
  24124.     charindex('7.00', @@version) > 0)
  24125.     begin
  24126.     if (exists (select * from sysobjects
  24127.         where name = 'sp_oledb_deflang' and type = 'P '))
  24128.         begin
  24129.             drop proc sp_oledb_deflang
  24130.             dump tran master with no_log
  24131.         end
  24132.     end
  24133. go
  24134.  
  24135. print ''
  24136. print 'creating sp_oledb_defdb'
  24137. go
  24138.  
  24139. if (charindex('8.00', @@version) = 0)
  24140. begin
  24141.     print ''
  24142.     print ''
  24143.     print 'Warning:'
  24144.     print 'you are installing the stored procedures '
  24145.     print 'on a pre 8.0 SQL Server.'
  24146.     print 'Ignore the following errors.'
  24147. end
  24148. go
  24149.  
  24150. create procedure sp_oledb_defdb
  24151. as
  24152.     begin
  24153.         select dbname from master..syslogins where sid=SUSER_SID()
  24154.     end
  24155. go
  24156.  
  24157. grant execute on sp_oledb_defdb to public
  24158. go
  24159.  
  24160. dump tran master with no_log
  24161. go
  24162.  
  24163. if (charindex('6.00', @@version) > 0 or
  24164.     charindex('6.50', @@version) > 0 or
  24165.     charindex('7.00', @@version) > 0)
  24166.     begin
  24167.     if (exists (select * from sysobjects
  24168.         where name = 'sp_oledb_defdb' and type = 'P '))
  24169.         begin
  24170.             drop proc sp_oledb_defdb
  24171.             dump tran master with no_log
  24172.         end
  24173.     end
  24174. go
  24175.  
  24176. print ''
  24177. print 'creating sp_oledb_database'
  24178. go
  24179.  
  24180. create procedure sp_oledb_database
  24181. as
  24182.     begin
  24183.         select name from master..sysdatabases
  24184.     end
  24185. go
  24186.  
  24187. grant execute on sp_oledb_database to public
  24188. go
  24189.  
  24190. dump tran master with no_log
  24191. go
  24192.  
  24193. if (charindex('6.00', @@version) > 0 or
  24194.     charindex('6.50', @@version) > 0 or
  24195.     charindex('7.00', @@version) > 0)
  24196.     begin
  24197.     if (exists (select * from sysobjects
  24198.         where name = 'sp_oledb_database' and type = 'P '))
  24199.         begin
  24200.             drop proc sp_oledb_database
  24201.             dump tran master with no_log
  24202.         end
  24203.     end
  24204. go
  24205.  
  24206. print ''
  24207. print 'creating sp_oledb_language'
  24208. go
  24209.  
  24210. create procedure sp_oledb_language
  24211. as
  24212. begin
  24213.     select 'English','us_english' 
  24214.     union 
  24215.     select alias,name from master..syslanguages
  24216. end
  24217. go
  24218.  
  24219. grant execute on sp_oledb_language to public
  24220. go
  24221.  
  24222. dump tran master with no_log
  24223. go
  24224. if (charindex('6.00', @@version) > 0 or
  24225.     charindex('6.50', @@version) > 0 or
  24226.     charindex('7.00', @@version) > 0)
  24227.     begin
  24228.     if (exists (select * from sysobjects
  24229.         where name = 'sp_oledb_language' and type = 'P '))
  24230.         begin
  24231.             drop proc sp_oledb_language
  24232.             dump tran master with no_log
  24233.         end
  24234.     end
  24235. go
  24236.  
  24237. /*---------------------------- END OLEDB CATALOG PROCS ------------------------*/
  24238.  
  24239.  
  24240. /*---------------------------- BEGIN BCP CATALOG PROCS ------------------------*/
  24241.  
  24242. print 'creating sp_tablecollations'
  24243. go
  24244.  
  24245. /* Used by BCP to gather all the collation names for a table */
  24246. if (charindex('8.00', @@version) = 0)
  24247. begin
  24248.     print ''
  24249.     print ''
  24250.     print 'Warning:'
  24251.     print 'you are installing the stored procedures '
  24252.     print 'on a pre 8.0 SQL Server.'
  24253.     print 'Ignore the following errors.'
  24254. end
  24255. go
  24256.  
  24257. /* Procedure for 8.0 server */
  24258. create procedure sp_tablecollations (@object nvarchar(4000))
  24259. as
  24260. begin
  24261.     select c.colid,
  24262.            c.name, 
  24263.            tds_collation = c.tdscollation,
  24264.            collation_name = BCPCollationName(c.tdscollation, c.xtype)
  24265.     from syscolumns c inner join sysobjects t on c.id = t.id
  24266.     where t.id = object_id(@object, 'local')
  24267.     order by c.colid
  24268. end
  24269. go
  24270.  
  24271. grant execute on sp_tablecollations to public
  24272. go
  24273.  
  24274. print 'creating sp_bcp_dbcmptlevel'
  24275. go
  24276.  
  24277. /* Used by BCP to get current db compatibility level */
  24278. if (charindex('8.00', @@version) = 0)
  24279. begin
  24280.     print ''
  24281.     print ''
  24282.     print 'Warning:'
  24283.     print 'you are installing the stored procedures '
  24284.     print 'on a pre 8.0 SQL Server.'
  24285.     print 'Ignore the following errors.'
  24286. end
  24287. go
  24288.  
  24289. /* Procedure for 8.0 server */
  24290. create procedure sp_bcp_dbcmptlevel (@dbname sysname)
  24291. as
  24292. begin
  24293.     select cmptlevel
  24294.     from master.dbo.sysdatabases
  24295.     where name = @dbname
  24296. end
  24297. go
  24298.  
  24299. grant execute on sp_bcp_dbcmptlevel to public
  24300. go
  24301.  
  24302. /*---------------------------- END BCP CATALOG PROCS ------------------------*/
  24303.  
  24304. dump tran master with no_log
  24305. go
  24306.  
  24307.  
  24308.  
  24309.  
  24310. if (exists (select * from sysobjects
  24311.         where name = 'sp_configure' and type = 'P '))
  24312.     begin
  24313.         exec sp_configure 'allow updates',0
  24314.         reconfigure with override
  24315.     end
  24316. go
  24317.  
  24318. exec sp_MS_upd_sysobj_category 2 /* set category | 2 based on crdate. */
  24319.  
  24320. go
  24321.  
  24322. if exists (select * from sysobjects where name = 'sp_check_objects'
  24323.             and type = 'P ')
  24324.     begin
  24325.         /* Only supported on 6.0 servers */
  24326.         print ''
  24327.         print 'Checking objects created by instcat.sql.'
  24328.  
  24329.         exec sp_check_objects 'catalog'
  24330.     end
  24331. go
  24332.  
  24333. print ''
  24334. print 'instcat.sql completed successfully.'
  24335. go
  24336.  
  24337. set quoted_identifier off
  24338. go
  24339.  
  24340. dump tran master with no_log
  24341. go
  24342. checkpoint
  24343. go
  24344. /**/
  24345.